12. Python isalpha()

发布时间:2023-12-08

Python isalpha()

更新:2022-07-24 13:06 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