Home / Python / Reference / String methods

Python string isdigit() method

Check whether every character is a digit (and there is at least one).

Syntax

str.isdigit()

Useful to check input before converting with int(). It is False for "-5" and "3.5" (the minus and the dot are not digits); for those, try the conversion inside try/except.

Parameters

None.

Return value

bool

Example

Code

for text in ["2026", "", "3.5", "-5", "12a"]:
    print(repr(text), text.isdigit())

Output

'2026' True
'' False
'3.5' False
'-5' False
'12a' False

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.