Python String isalnum()方法
isalnum() 方法检测字符串是否由字母和数字组成。
Python String isalnum() 语法
isalnum()方法语法:
str.isalnum()
Python String isalnum() 参数
- 无。
Python String isalnum() 返回值
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
Python String isalnum() 示例1
以下实例展示了isalnum()方法的实例:
string = "apidemos 123"
print(string, "is alphanumeric?", string.isalnum()) # apidemos 123 is alphanumeric? False
string = "apidemos_123"
print(string, "is alphanumeric?", string.isalnum()) # apidemos_123 is alphanumeric? False
string = "000"
print(string, "is alphanumeric?", string.isalnum()) # 000 is alphanumeric? True
string = "bbbb"
print(string, "is alphanumeric?", string.isalnum()) # bbbb is alphanumeric? True
# We can also use Python String isalnum() Method along with if…else statements, to output custom messages
password = "apidemos123456"
if password.isalnum():
print("Password is alphanumeric.")
else:
print("Password is not alphanumeric.")
输出: