Python 3 reference · String Methods
Python String isdecimal() Method
Return True if every character is a decimal digit (0–9 and their equivalents in other scripts). The strictest digit check.
Python 3 Since 3.0
Syntax
str.isdecimal()
Return value
True or False.
Examples
for s in ["123", "١٢٣", "²", "½", "-5"]:
print(repr(s), s.isdecimal(), s.isdigit(), s.isnumeric())
Output
'123' True True True '١٢٣' True True True '²' False True True '½' False False True '-5' False False False
text = "42"
if text.isdecimal():
print(int(text) + 1)
Output
43
Which one to use?
isdecimal() ⊂ isdigit() ⊂ isnumeric(). To check that int() will accept the text, isdecimal() is the
right test, but it rejects signs and spaces, so try: int(text) is often simpler.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.isdigit()· Return True if every character is a digit, including superscripts like ². Signs, spaces and decimal points return False.str.isnumeric()· Return True if every character is numeric, including digits, fractions like ½ and numerals from other scripts.- Python Strings · tutorial