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
S1 = '12345'       #纯数字
S2 = '①②'        #带圈的数字
S3 = '汉字'        #汉字
S4 = '%#¥'        #特殊符号

print(S1.isdigit())
print(S2.isdigit())
print(S3.isdigit())
print(S4.isdigit())

# 执行结果:
True     
True
False
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
S1 = 'abc汉字'     #汉字+字母
S2 = 'ab字134'     #包含数字
S3 = '*&&'         #特殊符号

print(S1.isalpha())
print(S2.isalpha())
print(S3.isalpha())

#执行结果
True
False
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
S1 = 'abc汉字1'    #字母+汉字+数字
S2 = '①②③'      #带圈的数字
S3 = '%……&'       #特殊符号

print(S1.isalnum())
print(S2.isalnum())
print(S3.isalnum())

#执行结果
True
True
False

注意点:

1.python官方定义中的字母:大家默认为英文字母+汉字即可

2.python官方定义中的数字:大家默认为阿拉伯数字+带圈的数字即可

相信只要理解到这两点,这三个函数的在使用时的具体返回值,大家就很明确了~~

猜你喜欢

转载自www.cnblogs.com/ilyou2049/p/11090067.html