Understanding isdecimal() Method in Python

Understanding isdecimal() Method in Python

3 mins read200 Views Comment
Updated on Oct 3, 2023 11:40 IST

This blog explains isdecimal() function in Python. We have explained this topic with the help of relevant examples.

2023_04_Copy-of-Copy-of-Feature-Image-Templates-26.jpg

When dealing with strings in Python, the isdecimal() method determines if all the characters in a string are decimal characters and returns True. If there are non-decimal characters, it returns False. 

In this article, we will discuss this function and understand its use with the help of examples. We will be covering the following sections: 

Explore free Python courses

Introduction to isdecimal() Method in Python 

In Python, isdecimal() is a built-in method that is used to check whether a given string is a decimal or not. It returns True if the string is a decimal, and False otherwise. 

The isdecimal() method is a part of the string class, so it can be used directly on any string variable or string literal. It checks whether the string contains only decimal characters or not. Decimal characters include digits from 0 to 9. 

Explore Online Python Courses

Syntax of isdecimal() 

The syntax of isdecimal() method in Python is given as: 

 
string.isdecimal()
Copy code

Parameters of isdecimal() 

The isdecimal() function does not take any parameters. 

Return Value of isdecimal() 

The isdecimal() method will give a True result if all of the characters in a string are decimal characters, and it will give a False result if there is at least one character that is not a decimal character. 

Types of Functions in Python
abs() Function in Python
Understanding casefold() Method in Python
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Examples of Using the isdecimal() Method in Python 

Example 1: Phone number validation 

Suppose we have a string that represents a phone number. We can use the isdecimal() method to check whether it contains only decimal characters or not. If it does, we can assume that it is a valid phone number. 

 
user_input = input("Enter a number: ")
if user_input.isdecimal():
print("Valid input")
else:
print("Invalid input")
Copy code

Output: 

Enter a number: 567839 
Valid input 

Example 2: Password validation 

Suppose we have a program that asks the user to enter a password. We can use the isdecimal() method to check whether the password contains at least one decimal character or not. If it does, we can assume that the password is strong enough. 

 
password = input("Enter a password: ")
if any(char.isdecimal() for char in password):
print("Strong password")
else:
print("Weak password")
Copy code

Output: 

Enter a password: dox123 
Strong password 

Example 3: Bank account number validation 

Suppose we have a string that represents a bank account number. We can use the isdecimal() method to check whether it contains only decimal characters or not. If it does, we can assume that it is a valid account number. 

 
account_number = "9876543210"
if account_number.isdecimal():
print("Valid account number")
else:
print("Invalid account number")
Copy code

Output: 

Valid account number  

In the above example, we check whether the account_number string is a valid bank account number by using the isdecimal() method. Since it contains only decimal characters, the method returns True, and the output is “Valid account number.”  

Example 4: Age validation 

Suppose we have a program that asks the user to enter their age. We can use the isdecimal() method to check whether the input contains only decimal characters or not. If it does, we can assume that the age is valid. 

 
age = input("Enter your age: ")
if age.isdecimal():
print("Valid age")
else:
print("Invalid age")
Copy code

Output: 

Enter your age: 3 
Valid age 

In the above example, we check whether the age input is valid or not by using the isdecimal() method. If the input contains only decimal characters, the output is “Valid age.” Otherwise, the output is “Invalid age.” 

Example 5: Postal code validation 

 
postal_code = "1234567"
if postal_code.isdecimal():
print("Valid postal code")
else:
print("Invalid postal code")
Copy code

Suppose we have a string that represents a postal code. We can use the isdecimal() method to check whether it contains only decimal characters or not. If it does, we can assume that it is a valid postal code. 

Output: 

Valid postal code  

In the above example, we check whether the postal_code string is a valid postal code by using the isdecimal() method. Since it contains only decimal characters, the method returns True, and the output is “Valid postal code.” 

Endnotes 

The isdecimal() method is a useful built-in function in Python that can be used to check whether a string contains only decimal characters or not. This method can be helpful in various applications, such as input validation, bank account number validation, and postal code validation. It is essential to note that the isdecimal() method only considers the characters from 0 to 9 as decimal characters and returns False if the string contains any other character. Moreover, it is important to use the appropriate method (isdecimal(), isdigit(), or isnumeric()) depending on the type of numeric characters that you want to consider. By understanding the functionality and limitations of the isdecimal() method, you can leverage its capabilities to improve the accuracy and reliability of your Python programs. 

Hope this article was helpful for you. If you wish to learn more about Python and practice Python programming, you can explore related articles here

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio