Tutorial Study Image

Python isalpha()

Python 中的 isalpha() 函数用于检查字符串中的所有字符是否都是字母。如果所有字符都是字母(可以是大写或小写),则该函数返回 True,否则返回 False。


string.isalpha() 
 

isalpha() 参数

isalpha() 不接受任何参数。此函数不允许任何特殊字符(如 ()!#%&?),甚至不允许空格。

isalpha() 返回值

isalpha() 方法还可以识别其他国际语言的 Unicode 字母。如果指定字符串为空,则 isalpha() 返回 False。

输入 返回值
如果全部是字母 True
如果至少有一个不是字母 False

Python 中 isalpha() 方法的示例

示例 1:Python 中 isalpha() 的工作原理


string = "python"
print(string.isalpha())

# contains whitespace
string = "Python programming"
print(string.isalpha())

# contains number
string = "123python"
print(string.isalpha())
 

输出


True
False
False

示例 2:isalpha() 与条件检查的工作原理


string = "Programming"

if string.isalpha() == True:
   print("All characters are alphabets")
else:
    print("All characters are not alphabets.")
 

输出


All characters are alphabets