简体   繁体   English

如何检查字符串是否仅包含大写或小写字母?

[英]How can I check if a string only contains uppercase or lowercase letters?

Return True if and only if there is at least one alphabetic character in s and the alphabetic characters in s are either all uppercase or all lowercase.当且仅当 s 中至少有一个字母字符且 s 中的字母字符全部大写或全部小写时,才返回 True。

def upper_lower(s):

   """ (str) -> bool



>>> upper_lower('abc')
True
>>> upper_lower('abcXYZ')
False
>>> upper_lower('XYZ')
True
"""

Regex is by far the most efficient way of doing this.正则表达式是迄今为止最有效的方法。

But if you'd like some pythonic flavor in your code, you can try this:但是如果你想在你的代码中加入一些 Python 风格,你可以试试这个:

'abc'.isupper()
'ABC'.isupper()
'abcABC'.isupper()

upper_lower = lambda s: s.isupper() or s.islower()

Use re.match使用re.match

if re.match(r'(?:[A-Z]+|[a-z]+)$', s):
    print("True")
else:
    print("Nah")

We don't need to add start of the line anchor since re.match tries to match from the beginning of the string.我们不需要添加行锚点的开头,因为re.match尝试从字符串的开头进行匹配。

So it enters in to the if block only if the input string contains only lowercase letters or only uppercase letters.因此,仅当输入字符串仅包含小写字母或仅包含大写字母时,它才会进入if块。

You can use the following regex:您可以使用以下正则表达式:

if re.search('^([a-z]+|[A-Z]+)$', s):
    # all upper or lower case

Explanation: ^ matches the beginning of the string, [az] and [AZ] are trivial matches, + matches one or more.说明: ^匹配字符串的开头, [az][AZ]是普通匹配, +匹配一个或多个。 | is the "OR" regex, finally $ matches the end of the string.是“OR”正则表达式,最后$匹配字符串的结尾。

Or, you can use all and check:或者,您可以使用all并检查:

all(c.islower() for c in s)
# same thing for isupper()

You could collect all alphabetic characters and then check if all or upper or all are lower您可以收集所有字母字符,然后检查是否全部或全部或全部低于

a=[c for c in s if s.isalpha()]
if a:
    if "".join(a).isupper() or "".join(a).islower():
    print "yes"   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查密码是否至少包含两个大写和两个小写字母 - Check that password contains at least two uppercase and two lowercase letters 在python中,你如何检查一个字符串是否有大写和小写字母 - In python, how do you check if a string has both uppercase and lowercase letters 如何检查字符串是否只包含字母? - How to check if a string only contains letters? 如何检查字符串是否包含字母表中的任何字母? - How can I check if a string contains ANY letters from the alphabet? 我必须检查字符串是否包含:字母数字、字母、数字、小写和大写字符 - I have to check if the string contains: alphanumeric, alphabetical , digits, lowercase and uppercase characters 如何用大写和小写字母打印每个 8 个字母的字符串? (在蟒蛇中) - How to print every 8-letter string with uppercase and lowercase letters? (In python) 将一些小写字母更改为字符串中的大写 - change some lowercase letters to uppercase in string 如何使用isalpha检查字符串是否只是python中的字母? - How can I check if a string is only letters in python using isalpha? 我可以识别小写和大写字母并将它们放入字典中。 我需要将大写和小写值连接在一起 - I can identify lowercase and uppercase letters and put them in a dictionary. I need to join the uppercase and lowercase values together 如何检查字符串是否包含字母? - How to check if a string contains letters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM