English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operation

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string isdigit() usage and example

Python string methods

If all characters in the string are numbers, the isdigit() method will return True. If not, it will return False.

The syntax of isdigit() is

string.isdigit()

isdigit() parameters

isdigit() does not accept any parameters.

isdigit() returns value

isdigit() returns:

  • True If all characters in the string are numbers.

  • False If at least one character is not a number.

Example1The work of isdigit()

s = "28212"
print(s.isdigit())
# Contains letters and spaces
s = "Mo3 nicaG ell22er"
print(s.isdigit())

When running the program, the output is:

True
False

Numbers are characters with property values:

  • Numeric_Type = number

  • Numeric_Type = decimal

In Python, superscripts and subscripts (usually written in unicode) are also considered numeric characters. Therefore, if the string contains these characters as well as decimal characters, then isdigit() returns True.

Roman numerals, currency denominations, and decimal points (usually written in unicode) are considered numeric characters, not numbers. If the string contains these characters, then isdigit() returns False.

To check if a character is a numeric character, you can use  isnumeric()Method.

Example2A string containing numbers and number characters

s = ''23455'
print(s.isdigit())
# s = '²'3455'
# Index is a number
s = '\u00B'23455'
print(s.isdigit())
# s = '½'
# Fraction is not a number
s = '\u00BD'
print(s.isdigit())

When running the program, the output is:

True
True
False

Python string methods