String isdecimal() in Python Example
Let’s explore some examples to understand how the isdecimal()
method works:
Python3
print ( "100" .isdecimal()) |
Output
True
String Containing digits and Numeric Characters
In Python, we can check if a string contains digit or numeric characters using isdecimal() method. Here is the Program to demonstrate the use of the Python String decimal() Method.
Python3
s = "12345" print (s.isdecimal()) # contains alphabets s = "12geeks34" print (s.isdecimal()) # contains numbers and spaces s = "12/34" print (s.isdecimal()) |
Output
True
False
False
Converting Numerical Strings to Integers using Isdecimal()
In Python, we can convert a string to an integer using isdecimal() method. Here is the Program to demonstrate the use of the Python String decimal() Method.
Python3
def convert_int(num_str): if num_str.isdecimal(): return int (num_str) else : return None print (convert_int( "555" )) print (convert_int( "11.11" )) |
Output
555
None
Python string isdecimal() Method
Python String isdecimal() function returns true if all characters in a string are decimal, else it returns False. In this article, we will explore further the isdecimal()
method, understand its functionality, and explore its practical applications in Python programming.