Python string isdigit() Method

In this tutorial, We will see the python string isdigit() method with examples.

isdigit() Method

The isdigit() method is one of the methods in a python string. Python string isdigit() method checks all the characters in the string are digits(0-9) or not.

  • Return True, If all the characters are digits.
  • Return False, If all the characters are not digits.
Syntax of Python string isdigit() method

This is the syntax of the python string isdigit() method.

string.isdigit()
Example of the Python string isdigit() method
#Example 1 

text = "1234"
#check the text contains only digit or not 
print(text.isdigit())
#output : True
#Example 2

text = "python3"
print(text.isdigit())

#output: False

#Exmaple 3

message = "123.9045!"
msg = message.isdigit()
print(msg)
# . and ! is not a digit.

#output: False

Program: Take a string from the user and check user input is a digit or not.

message = input("Enter phone number")
msg = message.isdigit()
print(msg)

Related Posts