1. “isdigit” 函數,判斷字符串中是否全部為“數字”。 如果字符串中有一個不是數字, 則為False
strs = "123" if strs.isdigit(): print("是數字") else: print("不是數字")
運行結果:
>>>是數字
2. 判斷字符串中是否包含“數字”, 使用for循環, 結合 isdigit 就可以辦到
strs = "中123文" for s in strs: if s.isdigit(): print("包含數字") break else: print("不包含數字")
運行結果:
包含數字
3. 其它字符串判斷函數。
str為字符串
str.isalnum() 所有字符都是數字或者字母
str.isalpha() 所有字符都是字母
str.isdigit() 所有字符都是數字
str.islower() 所有字符都是小寫
str.isupper() 所有字符都是大寫
str.istitle() 所有單詞都是首字母大寫,像標題
str.isspace() 所有字符都是空白字符、\t、\n、\r