Home >Backend Development >Python Tutorial >How to determine if a string is a number
Method functions for string processing in Python:
str.isnumeric(): True if only contains numbers; otherwise False. Note: This function can only be used for unicode string
str.isdigit(): True if it contains only digits; otherwise False.
str.isalpha(): True if only contains letters; otherwise False.
str.isalnum(): True if only contains letters or numbers; otherwise False.
Example string:
str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"
Code processing process:
#用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
Note: If the string contains characters other than letters or numbers, such as spaces, It will also return False
Strict analysis: If there are symbols other than numbers or letters (spaces, semicolons, etc.), it will be False
isalnum() must be numbers and letters Mixing
isalpha() is not case sensitive
The above is the detailed content of How to determine if a string is a number. For more information, please follow other related articles on the PHP Chinese website!