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

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations de fichiers Python

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string isdecimal() usage and example

Python string methods

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

The syntax of isdecimal() is

string.isdecimal()

isdecimal() parameters

isdecimal() does not accept any parameters.

isdecimal() return value

isdecimal() returns:

  • True If all characters in the string are decimal characters.

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

Example1: The work of isdecimal()

s = "28212"
print(s.isdecimal())
# Contains letters
s = "32ladk3"
print(s.isdecimal())
# Contains letters and spaces
s = "Mo3 nicaG el l22er"
print(s.isdecimal())

When running the program, the output is:

True
False
False

Superscripts and subscripts are considered number characters, not decimals. If the string contains these characters (usually written using unicode), isdecimal() returns False.

Similarly, Roman numerals, currency denominations and fractions are considered numbers (usually written using unicode), not decimals. In this example, isdecimal() also returns False.

There are two methods, dinst digit() is used to check if the string is composed only of numbers and isnumeric() method detects if the string is composed only of numbers, this method is only for unicode objects.

Learn aboutisdigit()andisnumeric()More information about the method.

Example2String containing numbers and number characters

s = ''23455'
print(s.isdecimal())
#s = '²'3455'
s = '\u00B'23455'
print(s.isdecimal())
# s = '½'
s = '\u00BD'
print(s.isdecimal())

When running the program, the output is:

True
False
False

Python string methods