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 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())

The output when running the program is:

True
False
False

Superscripts and subscripts are considered numeric characters, rather than 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), rather than decimals. In this example, isdecimal() also returns False.

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

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

Example2String containing numbers and numeric characters

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

The output when running the program is:

True
False
False

Python string methods