【python入門到放棄】isdigit()、isalpha()、isalnum() 三個函數的區別和注意點


 一、isdigit()

python關於 isdigit() 內置函數的官方定義:
S.isdigit() -> bool
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
翻譯:
S.isdigit()返回的是布爾值:True False
S中至少有一個字符且如果S中的所有字符都是數字,那么返回結果就是True;否則,就返回False
 1 S1 = '12345'       #純數字
 2 S2 = '①②'        #帶圈的數字
 3 S3 = '漢字'        #漢字
 4 S4 = '%#¥'        #特殊符號
 5 
 6 print(S1.isdigit())
 7 print(S2.isdigit())
 8 print(S3.isdigit())
 9 print(S4.isdigit())
10 
11 # 執行結果:
12 True     
13 True
14 False
15 False
 
        

 二、isalpha()

python關於 isalpha() 內置函數的官方定義:
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
翻譯:
S.isalpha()返回的是布爾值:True False
S中至少有一個字符且如果S中的所有字符都是字母,那么返回結果就是True;否則,就返回False
 1 S1 = 'abc漢字'     #漢字+字母
 2 S2 = 'ab字134'     #包含數字
 3 S3 = '*&&'         #特殊符號
 4 
 5 print(S1.isalpha())
 6 print(S2.isalpha())
 7 print(S3.isalpha())
 8 
 9 #執行結果
10 True
11 False
12 False

三、isalnum()
python關於 isalnum() 內置函數的官方定義:
S.isalnum() -> bool
Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
翻譯:
S.isalnum()返回的是布爾值:True False
S中至少有一個字符且如果S中的所有字符都是字母數字,那么返回結果就是True;否則,就返回False
 1 S1 = 'abc漢字1'    #字母+漢字+數字
 2 S2 = '①②③'      #帶圈的數字
 3 S3 = '%……&'       #特殊符號
 4 
 5 print(S1.isalnum())
 6 print(S2.isalnum())
 7 print(S3.isalnum())
 8 
 9 #執行結果
10 True
11 True
12 False

 

注意點:

1.python官方定義中的字母:大家默認為英文字母+漢字即可

2.python官方定義中的數字:大家默認為阿拉伯數字+帶圈的數字即可

相信只要理解到這兩點,這三個函數的在使用時的具體返回值,大家就很明確了~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM