python - 如何检查字符串是否只包含字母?

标签 python string

我正在尝试检查一个字符串是否只包含字母,而不是数字或符号。

例如:

>>> 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/

相关文章:

java - java的iterator.remove()如何翻译成python?

python - 为什么多个 list.count 调用比单个循环更快?

python - 使用 tensorflow 运行线性回归时出现类型错误。 ' Op has type float64 that does not match type float32 of argument'

Python Django Gmail SMTP 设置

c++ - 字节缓冲区到字符串

Java .split - 删除第一个匹配项

string - 在 Swift 中重新排序字符串字符

python - Tkinter 与 openweather

java - 如何修复我用compareTo()比较字符串的代码

python - 为什么某些代码在 Python2 中是确定性的,而在 Python 3 中是非确定性的?