Python知識(shí)分享網(wǎng) - 專業(yè)的Python學(xué)習(xí)網(wǎng)站 學(xué)Python,上Python222
Python islower()函數(shù)詳解
發(fā)布于:2023-07-17 15:33:43

 

islower

 

islower() 可以判斷「字符串」是否由「小寫」字母組成。

語法

 

string.islower()

 

返回值

字符串中所有字母全是小寫,就返回True

字符串中不包含字母或者字母不是全部小寫,就返回False

實(shí)例:判斷字符串是不是純小寫

 

print('hello world'.islower())

 

輸出:

 

True

 

1、包含數(shù)字的情況

字符串只包含「數(shù)字」返回 False

 

print('123'.islower())

 

輸出:

 

False

 

字符串同時(shí)包含「數(shù)字」「小寫字母」返回 True

 

print('123abc'.islower())

 

輸出:

 

True

 

2、包含特殊符號(hào)的情況

字符串只包含「特殊符號(hào)」返回False

 

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

 

輸出:

 

False

 

字符串同時(shí)包含「特殊符號(hào)」「小寫字母」返回True

 

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

 

輸出:

 

True
True

 

3、包含漢字的情況

字符串只包含「漢字」返回False

 

print('漢字'.islower())

 

輸出:

 

False

 

字符串同時(shí)包含「漢字」「小寫字母」返回True

 

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

 

輸出:

 

True
True

 

4、包含空格的情況

字符串只包含「空格」返回False

 

print('  '.islower())

 

輸出:

 

False

 

字符串同時(shí)包含「空格」「小寫字母」返回True

 

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

 

輸出:

 

True

 

5、其他國家的語言

其他國家語言的小寫字母,也會(huì)返回True。

比如:「希臘字母」

大寫:ΑΒΓΔΕΖ

小寫:αβγδεζ

 

print('ΑΒΓΔΕΖ'.islower())
print('αβγδεζ'.islower())

 

輸出:

 

False
True

 

「俄語」

大寫:АБВГДЕ

小寫:абвгде

 

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

 

輸出:

 

False
True

 

6、判斷純數(shù)字

如果你有一個(gè)需求,想要判斷用戶輸入的字符串,是不是只包含小寫字母,那么 islower() 是不符合預(yù)期的,可以配合「循環(huán)」,逐個(gè)字符判斷:

 

str1 = '123abc'

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

if(Myislower( str )):
    print('純數(shù)字')
else:
    print('非純數(shù)字')

 

輸出:

 

非純數(shù)字

 

轉(zhuǎn)載自:https://blog.csdn.net/wangyuxiang946/article/details/131570060