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

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operation

Python objects and classes

Python date and time

Advanced knowledge of Python

Python Reference Manual

Usage and example of Python string isdigit()

String methods in Python

If all the 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 the characters in the string are numbers.

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

Example1How does isdigit() work

s = "28212"
print(s.isdigit())
# Contains letters and spaces
s = "Mo3 nicaGél22er"
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, isdigit() returns True.

Roman numerals, currency denominators, and decimals (usually written in unicode) are considered numeric characters rather than numbers. If the string contains these characters, 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'
# The index is a number
s = '\u00B'23455'
print(s.isdigit())
# s = '\u00B'
# The fraction is not a number
s = '\u00BD'
print(s.isdigit())

When running the program, the output is:

True
True
False

String methods in Python