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

isdecimal vs isdigit vs isnumericTry it yourself
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
Safe to pass to int()Try it yourself
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

VersionChange
Python 3.0Added