How to determine if a string is a number

anonymity
Release: 2019-05-25 11:35:51
Original
12918 people have browsed it

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.

How to determine if a string is a number

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!