Detailed explanation of Python islower() function

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

islower() can determine whether the "string" is composed of "lowercase" letters.

grammar

string.islower()

return value

  • Returns True if all letters in the string are lowercase
  • If the string does not contain letters or the letters are not all lowercase, return False

Example: Determine whether a string is pure lowercase

print('hello world'.islower())

output:

True

1. Cases containing numbers

String contains only "numbers" returns False

print('123'.islower())

output:

False

Returns True if the string contains both "numbers" and "lowercase letters"

print('123abc'.islower())

output:

True

2. Cases containing special symbols

String contains only "special symbols" returns False

print('~!@#$%^&*_+'.islower())

output:

False

Return True if the string contains both "special symbols" and "lowercase letters"

print('ab~!@#$%^&*_+'.islower())
print('~!@#$%^&*_+ab'.islower())

output:

True
True

3. The case of including Chinese characters

String contains only "Chinese characters" returns False

print('汉字'.islower())

output:

False

Return True if the string contains both "Chinese characters" and "lowercase letters"

print('abc汉字'.islower())
print('汉字abc'.islower())

output:

True
True

4. The case of including spaces

String contains only "spaces" returns False

print('  '.islower())

output:

False

Return True if the string contains both "space" and "lowercase letters"

print('a b c'.islower())

output:

True

5. Languages ​​of other countries

Lowercase letters in other national languages ​​will also return True.

For example: "Greek letters"

  • 大写:ABGDEZ
  • 小写:Avgdez
print('ΑΒΓΔΕΖ'.islower())
print('αβγδεζ'.islower())

output:

False
True

"Russian"

  • Uppercase: АБВГДЕ
  • Lowercase: абвгде
print('АБВГДЕ'.islower())
print('абвгде'.islower())

output:

False
True

6. Judging pure numbers

If you have a requirement and want to judge whether the string entered by the user contains only lowercase letters, then islower() does not meet expectations. You can cooperate with "loop" to judge character by character:

str1 = '123abc'

def Myislower( str ):
    for x in str1:
        if x.islower():
            return True
        else:
            return False

if(Myislower( str )):
    print('纯数字')
else:
    print('非纯数字')

output:

非纯数字

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131570060