AppDividend
Latest Code Tutorials

Python String isdecimal() Method Example

Python string isdecimal() is an inbuilt method that is used to check whether the given string consists of only decimals. Python isdecimal() method returns true if the string has its all character as a decimal and false otherwise. In other words, we can say that it checks if the characters present in the string are decimal or not.  So, if you want to check the decimals in the string, then the Python isdecimal() method is beneficial.

Python String isdecimal()

It is a function in Python that returns True if all characters in a string are decimal. If all characters are not decimal, then it returns False.

Syntax

string.isdecimal()

The isdecimal() function doesn’t take any parameters. There will error shown if we try to pass any parameter to the method.

It returns true if the string consists only of decimals.

It returns False if the string doesn’t contain decimals or contains characters other than decimals like numerical or special characters. When it identifies a space, then also it returns False.

Programs on isdecimal() method

Write a program to demonstrate the use of the isdecimal() method.

# app.py

string = "123456"
print(string.isdecimal())

Output

➜  pyt python3 app.py
True
➜  pyt

Write a program to check multiple strings using the isdecimal() method.

# app.py

string = "123456"
string2 = "123 456"
print(string.isdecimal())
print(string2.isdecimal())

Output

➜  pyt python3 app.py
True
False
➜  pyt

The superscript and subscripts are considered digit characters but not decimals. If the string contains these characters (usually written using Unicode), isdecimal() returns False.

See the following code.

# app.py

string = "123456"
string2 = "19k21k"
string3 = "11m 21k"
print(string.isdecimal())
print(string2.isdecimal())
print(string3.isdecimal())

Output

➜  pyt python3 app.py
True
False
False
➜  pyt

Similarly, roman numerals, currency numerators, and fractions are considered numeric numbers (usually written using Unicode) but not decimals. The isdecimal() also returns False.

There are two methods isdigit() and isnumeric() that check whether the string contains digit characters and numeric characters, respectively.

String Containing digits and Numeric Characters

See the following code.

# app.py

string = '\u00B48155'
string2 = '\u00BD'
print(string.isdecimal())
print(string2.isdecimal())

Output

➜  pyt python3 app.py
False
False
➜  pyt

Finally, Python String isdecimal() Method Example is over.

Related Posts

Python String capitalize()

Python String casefold()

Python String expandtabs()

Python String center()

Python String endswith()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.