EN
Python - check if string only contains numbers
0 points
In this article, we would like to show you how to check if string only contains numbers in Python.
Quick solution:
xxxxxxxxxx
1
text = "123"
2
print(text.isdigit()) # True
xxxxxxxxxx
1
text = "123a"
2
print(text.isdigit()) # False
In this example, we use string.isdigit()
method to check if the strings contain only numbers.
xxxxxxxxxx
1
numbers = "123"
2
print(numbers.isdigit()) # True
3
4
text = "abc"
5
print(text.isdigit()) # False
6
7
float_number = "1.23"
8
print(float_number.isdigit()) # False
Output:
xxxxxxxxxx
1
True
2
False
3
False
Note:
You can also use
isdecimal()
instead ofisdigit()
.