.isdecimal()
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:
- The string is not empty (has at least one character).
- 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
.
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:
TrueFalseFalseTrue
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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours