Python String isdigit()
Summary: in this tutorial, you’ll learn how to use the Python string isdigit()
method to determine if all characters in a string are digits.
Introduction to the Python String isdigit() method
The string isdigit()
method returns True
if:
1. All characters in the string are digits and
2. The string has at least one character.
Otherwise, it returns False
.
The following shows the syntax if the string isdigit()
method:
str.isdigit()
The isdigit()
method returns True if the string contains solely the digits (0-9). If a string contains a decimal point, minus, or plus sign, the isdigit()
returns False
.
Note that the isdigit()
also returns True for a string that contains superscript like ’10²’.
Python String isdigit() method examples
The following example uses the isdigit()
method to check if the all characters in a string are digits:
quantity = '100'
print(quantity.isdigit())
Output:
True
The following example returns False
:
price = '9.99'
print(price.isdigit())
Output:
False
The following examples returns True
because the Unicode character for 0 and 2 are digits:
x = "\u0030" # unicode for 0
y = "\u00B2" # unicode for ²print(x.isdigit())
print(y.isdigit())
Output:
True
True
The following example returns False
because the string is empty:
empty = ''
print(empty.isdigit())
Output:
False
Summary
- Use the Python string
isdigit()
method to check if all characters in a string are digits.