Python String isdecimal()

The Python String isdecimal() method is a built-in function that returns true if all the characters in a string are decimal. If one of the characters is not decimal in the string, it returns false.

In this article, we will learn about the Python String isdecimal() method with the help of examples.

isdecimal() Syntax

The Syntax of isdecimal() method is:

string.isdecimal()

isdecimal() Parameters

The isdecimal() method does not take any parameters.

isdecimal() Return Value

The isdecimal() method returns

  • True if all the characters in a string are valid decimal characters.
  • False if one or more characters in a string are not decimal characters.

Example 1: Working of isdecimal()


# Python3 program to demonstrate the use
# of isdecimal() 
  
s = "12345"
print(s.isdecimal())
  
# contains alphabets
s = "123Hello123"
print(s.isdecimal())
  
# contains numbers and spaces
s = "12345 6789"
print(s.isdecimal())

Output

True
False
False

Example 2: String Containing digits and Numeric Characters

The superscript and subscript are considered as digit characters and not decimals. If the string contains subscript or superscript the isdecimal() method will returns False.

Similarly, the roman numbers, currencies and fractions are considered as numeric numbers and not decimals. The isdecimal() will return False if it finds these characters.

It is recommended to use isdigit() method and isnumeric() method to check if the characters are valid digits and numeric characters respectively.


# Python3 program to demonstrate the use
# of isdecimal() 

# vaid decimal  
s = '12345'
print(s.isdecimal())

# in case of digit
#s = '²123'
s = '\u00B2123'
print(s.isdecimal())

# incase of numeric
# s = '½'
s = '\u00BD'
print(s.isdecimal())

Output

True
False
False

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python List Pop()

Python List pop()

Table of Contents Hide Syntax of List pop() pop() ParametersReturn Value from List pop()Example 1: Pop item at the given index from the listExample 2: pop() without an index, and for…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post
Python String Index()

Python String index()

Table of Contents Hide index() Syntaxindex() Parametersindex() Return ValueDifference between index() method and find() methodExample 1: Find the index of a string in PythonExample 2: If string is not found…
View Post