Python String isdecimal() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String isdecimal() method

The isdecimal() method in Python returns True if all the characters in the string are decimal characters. For empty strings, it returns False.

What are Decimal characters?

Any character that can be used to form a number in base 10, is a decimal character.

Example:- 1. All numbers from 0 to 9 2. All numbers from 0 to 9 in various languages like Arabic Indic, Devnagri etc.

You can find all the decimal characters in Unicode General Category Nd.

Syntax of isdecimal() method
string.isdecimal()

Returns True or False
Example

Code/Output

s = "1992"
print(s.isdecimal())
>>> True

# Floating point number
s = "1992.10"
print(s.isdecimal())
>>> False

# contains alphabets
s = "Pylenin1992"
print(s.isdecimal())
>>> False

# contains alphabets and spaces
s = "Pylenin loves Python"
print(s.isdecimal())
>>> False

# Empty string
s = ''
print(s.isdecimal())
>>> False

# superscripts
s = '1992\u00B2'
print(s)
print(s.isdecimal())
>>> 1992 ^ 2
>>> False

# fractions
s = '\u00BD'
print(s)
print(s.isdecimal())
>>> 1/2
>>> False
Difference between isdigit() and isdecimal()

Difference between the above methods crop up with various unicode characters, such as superscripts:

# superscripts
s = '2345\u00B2'
print(s)

print(s.isdecimal())
print(s.isdigit())

Output

2345^2
False #isdecimal()
True #isdigit()

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide