断言字符串只包含字母

2024-04-29 13:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我见过像isAlpha()这样的方法,但它接受空格和标点符号,这是我不想要的。有什么方法可以检查字符串是否只包含大写字母或大写字母?在

例如,psudo:

"asdf".isLetters() -> true

"as df".isLetters() -> false
"as. df:".isLetters() -> false

Tags: 方法字符串falsetruedfas大写字母空格
2条回答
>>> "asdf".isalpha()
True
>>> "as df".isalpha()
False
>>> "as. df:".isalpha()
False

根据documentation for ^{}的说法,它做的似乎是你想要的:

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

要检查大写字母,请使用my_str.isupper()

import re

if re.match(r"^[A-Za-z]*$", some_string):
    print "yey"!

相关问题 更多 >