Python Program To Check user Input is a Number or Letter

Python check if input is number or letter; Through this tutorial, i am going to show you how to check if user input is a number, letter or string in python.

Python Program To Check user Input is a Number or Letter

  • How to check if a string contains numbers in python
  • How to check if a string contains letters and numbers in python

How to check if a string contains numbers in python

  • Get input value from the user in program.
  • Check whether a string contains number or not by using isdigit() function.
  • Print the result.
# python code to check whether a string 
# contains only digits or not 
#
str1 = input("Please enter number or string or both")
# checking & printing messages
if str1.isdigit():
    print("str contains a number")
else:
    print("str does not contain a number")

After executing the program, the output will be:

Execution 1 of program check if a string contains numbers in python:

Please enter number or string or both 1234
str contains a number

Execution 2 of program check if a string contains numbers in python:

Please enter number or string or both test556
str does not contain a number

How to check if a string contains letters in python

  • Get input value from the user in python.
  • Check whether a string contains letter or not by using isalpha() function.
  • Print the result.
# python code to check whether a string 
# contains only letter or not 
#
str1 = input("Please enter anything ")
# Code to check
if str1.isalpha(): 
    print("String contains only letters") 
else: 
    print("String doesn't contains only letters") 

After executing the program, the output will be:

Execution 1 of program check if a string contains letter in python:

Please enter anything here
String contains only letters

Execution 2 of program check if a string contains numbers in python:

Please enter anything  hello34
String doesn't contains only letters

Recommended Python Tutorials

Leave a Comment