
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to check if a string only contains certain characters in Python?
- How to check if a unicode string contains only numeric characters in Python?
- Java Program to check if the String contains only certain characters
- Python Program to check if String contains only Defined Characters using Regex
- How to check if a Python string contains only digits?
- Check whether the String contains only digit characters in Java
- How to check if a string contains only whitespace letters in Python?
- Check if string contains special characters in Swift
- Python program to check if a string contains all unique characters
- How to check if the string contains only digits in JavaScript?
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only upper case letters in Python?
- How to check if a string contains only one type of character in R?
- Check if a string contains only alphabets in Java using Regex
- Is it possible to check if a String only contains ASCII in java?
