EN
Python - check if string contains only digits
0 points
In this article, we would like to show you how to check if string contains only digits 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 digits.
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()
method instead ofisdigit()
.