isalnum
1)isalpha()方法 (1 ) isalpha() Method)
isalpha() method returns true if all characters of a string are only alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’). If there is any non alphabet character in the string, method returns false.
如果字符串的所有字符都是字母(“ A”至“ Z”之间以及“ a”至“ z”之间),则isalpha()方法返回true 。 如果字符串中有任何非字母字符,则方法返回false 。
Syntax:
句法:
string.isalpha()
Parameter: None
参数:无
Return type:
返回类型:
true - if all characters of the string are alphabets.
true-如果字符串的所有字符都是字母。
false - if any of the character of the string is not an alphabet (like number, space, special characters).
false-如果字符串中的任何字符都不是字母(例如数字,空格,特殊字符)。
Example:
例:
# str1 with only alphabets
str1 = "Helloworld"
# str2 with alphabets, space and special character
str2 = "Hello world!"
# str3 with alphabets, space and numbers
str3 = "Hello world 100"
# check whether string contains only alphabets or not
print str1.isalpha ()
print str2.isalpha ()
print str3.isalpha ()
Output
输出量
True
False
False
2)isalnum()方法 (2) isalnum() Method)
isalnum() method returns true if all characters of a string are either alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’) or numbers (0 to 9) or both, otherwise method returns false.
如果字符串的所有字符都是字母(介于“ A”至“ Z”之间,介于“ a”至“ z”之间)或数字(0至9)或两者皆有,则isalnum()方法返回true ,否则方法返回false 。
Syntax:
句法:
string.isalnum()
Parameter: None
参数:无
Return type:
返回类型:
true - if all characters of the string are alphabets or/and numbers.
true-如果字符串的所有字符都是字母或数字。
false - if any of the character of the string is not either an alphabet or a number.
false-如果字符串中的任何字符都不是字母或数字。
Example:
例:
# str1 with alphabets and numbers
str1 = "Helloworld123"
# str2 with only alphabets
str2 = "Helloworld"
# str3 with numbers only
str3 = "12345"
# str4 with alpa numeric characters and space
str4 = "Amit Shukla 21"
# check whether string contains only alphabets or not
print str1.isalnum ()
print str2.isalnum ()
print str3.isalnum ()
print str4.isalnum ()
Output
输出量
True
True
True
False
翻译自: https://www.includehelp.com/python/string-isalnum-and-isalpha-methods-with-examples.aspx
isalnum