Python isdigit() method returns True if all the characters in the string are digits. It returns False if no character is digit in the string.
Python String isdigit() Method Syntax
It has the following syntax:
isdigit()
Parameters
No parameter is required.
Return
It returns either True or False.
Different Examples for Python String isdigit() Method
Let’s see some examples of isdigit() method to understand it’s functionalities.
Example 1
A simple example of digit testing using isdigit() method.
# Python isdigit() method example
# Variable declaration
str = '12345'
# Calling function
str2 = str.isdigit()
# Displaying result
print(str2)
Output:
True
Example 2
It returns true only if all the characters are digit. See the example below.
# Python isdigit() method example
# Variable declaration
str = "12345"
str3 = "120-2569-854"
# Calling function
str2 = str.isdigit()
str4 = str3.isdigit()
# Displaying result
print(str2)
print(str4)
Output:
True
False
Example 3
We can use it in programming to check whether a string contains digits or not.
# Python isdigit() method example
# Variable declaration
str = "123!@#$"
if str.isdigit() == True:
print("String is digit")
else:
print("String is not digit")
Output:
String is not digit
Leave a Reply