我正在尝试检查一个字符串是否只包含字母,而不是数字或符号。
例如:
>>> only_letters("hello")
True
>>> only_letters("he7lo")
False
最佳答案
简单:
if string.isalpha():
print("It's all letters")
str.isalpha()
仅当字符串中的所有个字符都是字母时才为真:
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
演示:
>>> 'hello'.isalpha()
True
>>> '42hello'.isalpha()
False
>>> 'hel lo'.isalpha()
False
关于python - 如何检查字符串是否只包含字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667410/