1. 程式人生 > >Python判斷字符串是否為字母或者數字

Python判斷字符串是否為字母或者數字

bar spa src ace 技術分享 str detail upper title

嚴格解析:有除了數字或者字母外的符號(空格,分號,etc.)都會False
isalnum()必須是數字和字母的混合
isalpha()不區分大小寫
技術分享圖片
str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"

#用isdigit函數判斷是否數字
print(str_1.isdigit())
Ture
print(str_2.isdigit())
False
print(str_3.isdigit())
False

#用isalpha判斷是否字母
print(str_1.isalpha())    
False
print(str_2.isalpha())
Ture    
print(str_3.isalpha())    
False

#isalnum判斷是否數字和字母的組合
print(str_1.isalnum())    
Ture
print(str_2.isalnum())
Ture
print(str_1.isalnum())    
Ture
註意:如果字符串中含有除了字母或者數字之外的字符,比如空格,也會返回False

================

python3判斷字符串是字母/數字/大小寫的系統函數:

函數 含義
字符串.isalnum() 所有字符都是數字或者字母,為真返回 Ture,否則返回 False。
字符串.isalpha() 所有字符都是字母,為真返回 Ture,否則返回 False。
字符串.isdigit() 所有字符都是數字,為真返回 Ture,否則返回 False。
字符串.islower() 所有字符都是小寫,為真返回 Ture,否則返回 False。
字符串.isupper() 所有字符都是大寫,為真返回 Ture,否則返回 False。
字符串.istitle() 所有單詞都是首字母大寫,為真返回 Ture,否則返回 False。
字符串.isspace() 所有字符都是空白字符,為真返回 Ture,否則返回 False。
---------------------
作者:第一行Python代碼
來源:CSDN
原文:https://blog.csdn.net/godot06/article/details/80966646
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

Python判斷字符串是否為字母或者數字