Python .isdecimal()

noahpgordon's avatar
Published Sep 27, 2023Updated Oct 6, 2023
Contribute to Docs

The method .isdecimal() takes a string parameter and returns True if and only if the following two conditions are met:

  1. The string is not empty (has at least one character).
  2. All of the characters in the string are decimal characters.

Otherwise, .isdecimal() returns False.

A decimal character is a character that represents an integer from 0 to 9.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

.isdecimal() can be called in two ways:

string_variable.isdecimal()

OR

str.isdecimal(string_variable)

Where string_variable is the string parameter.

Example

print("783".isdecimal())
print("not a decimal".isdecimal())
print(str.isdecimal(""))
print(str.isdecimal("\u0033"))

The above gives the following output:

True
False
False
True

Note that the fourth call to .isdecimal() returns True because "\u0033" is the unicode encoding of the decimal character "3".

Codebyte Example

The code below is runnable, change the value of your_string to test the .isdecimal() method.

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours