python string isalpha() 方法用于检查字符串是否由字母组成。如果输入字符串中的所有字符都是字母格式的,并且至少有一个字符,则此方法返回 true。否则,它将返回 false。
字母字符是在 Unicode 字符数据库中定义为 “Letter” 的字符,即具有常规类别属性的字符是 “Lm”、“Lt”、“Lu”、“Ll” 或 “Lo” 之一的字符。请注意,这与 Unicode 标准中定义的 “Alphabetic” 属性不同。
让我们在下一节中更详细地研究这个函数。
语法
以下是 python string isalpha() 方法的语法 -
str.isalpha()
参数
python string isalpha() 方法不包含任何参数。
返回值
如果字符串中的所有字符都是字母格式的,并且 至少是一个字符,否则为 false。
例小写字母表和大写字母表属于字母字符。其他字符,例如 '!'。'@', ... 不被视为字母顺序。
str = "Hello!welcome"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)
在执行上述程序时,将生成以下输出 -
Are all the characters of the string alphabetic? False
例
只有小写字母和大写字母属于字母字符。
以下是 python 字符串 isalpha() 方法的示例。在此程序中,将创建一个字符串 “Welcome” 并调用 isalpha() 函数。
str = "Welcome"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)
以下是执行上述程序得到的输出 -
Are all the characters of the string alphabetic? True
例
只有小写字母和大写字母属于字母字符。即使是空白的空格 “ ” 也不被视为字母。
str = "welcome "
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)
通过执行上述程序获得以下输出 -
Are all the characters of the string alphabetic? False
例
只有小写字母和大写字母属于字母字符。
str = "aBCD"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)
上述程序在执行时显示以下输出 -
Are all the characters of the string alphabetic? True
例
小写字母表和大写字母表被视为字母字符。甚至数字和空白处 不被视为字母顺序。
str = "Welcome to qikepu12"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)
上述程序的输出显示如下 -
Are all the characters of the string alphabetic? False