- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a string contains only decimal characters?
A string is a group of characters that may be used to represent a single word or an entire sentence. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier.
In Python strings are represented by the class named string and this class provides several built-in methods to manipulate and access strings.
In this article, we are focusing on how to check if a string contains only decimal characters in Python.
Using the isdigit() function
One way to achieve this is using the inbuilt string function isdigit(). The function takes the input as a string and returns true if all the characters present in the string are digits otherwise it returns false. The main drawback of this function is that it returns False if there are any decimal characters or any negative number is present.
Example 1
In the program given below, we are taking 2 strings as input and checking if they are having only decimal characters in them or not using the isdigit() method.
str1 = "12345" str2 = "1234@#" print("Checking if the string '",str1,"' has only decimal characters") print(str1.isdigit()) print("Checking if the string '",str2,"' has only decimal characters") print(str2.isdigit())
Output
The output of the above example is,
("Checking if the string '", '12345', "' has only decimal characters") True ("Checking if the string '", '1234@#', "' has only decimal characters") False
Example 2
In the example given below, we are taking 2 strings as input and we are taking the inputs with a decimal point and a negative sign and checking if they are decimal characters using the isdigit() approach.
str1 = "123.45" str2 = " 12345" print("Checking if the string '",str1,"' has only decimal characters") print(str1.isdigit()) print("Checking if the string '",str2,"' has only decimal characters") print(str2.isdigit())
Output
The output of the above example is,
("Checking if the string '", '123.45', "' has only decimal characters") False ("Checking if the string '", ' 12345', "' has only decimal characters") False
Solution of the above drawback
To resolve the drawback of the above approach lets create a user defined function to overcome the drawback caused by isdigit(). To overcome the decimal number drawback, we will split the string when we have a ‘.’ Using the split function. To overcome the negative number drawback, we will strip the ‘ ’ character using the strip function.
Example
In the example given below, we are writing an inbuilt function and eliminating ‘.’ and ‘ ’ using strip() and split() functions and checking if the string contains only decimal characters.
def isfloat(str): s1 = str.lstrip(' ') s2 = s1.split('.') return all(n.isdigit() for n in s2) and len(s2) <= 2 str1 = "123.45" str2 = " 12345" print("Checking if the string '",str1,"' has only decimal characters") print(isfloat(str1)) print("Checking if the string '",str2,"' has only decimal characters") print(isfloat(str2))
Output
The output of the above example is,
("Checking if the string '", '123.45', "' has only decimal characters") True ("Checking if the string '", ' 12345', "' has only decimal characters") True
Using regular Expressions
Another way to achieve this is using Regular Expressions. The regular expression "^\d+?\.\d+?$" is used for checking if there are only digits present. The regular expression function match is used for checking.
Example
In the example given below, we are using regular expressions and finding out whether the given string contains only decimal characters.
import re str1 = "123.45" str2 = "123@45" print("Checking if the string '",str1,"' has only decimal characters") print(bool(re.match("^\d+?\.\d+?$", str1))) print("Checking if the string '",str2,"' has only decimal characters") print(bool(re.match("^\d+?\.\d+?$", str2)))
Output
The output of the above example is,
("Checking if the string '", '123.45', "' has only decimal characters") True ("Checking if the string '", '123@45', "' has only decimal characters") False