Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython Check User input is a Letter or Number

Python Check User input is a Letter or Number

Python program to check if the input is number or letter; In this tutorial, you will learn how to check if the input is a number or letter in python.

.medrectangle-3-multi-320{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Python Check User input is a Letter or Number

  • Python code to check whether a string contains a number or not
  • Python check if given string contains only letter

Python code to check whether a string contains a number or not

  • Take input from the user.
  • 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

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

Execution -2

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

Python check if given string contains only letter

  • Take input from the user.
  • 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

Please enter anything here
String contains only letters

Execution – 2

Please enter anything  hello34
String doesn't contains only letters

Recommended Python Tutorials

.banner-1-multi-360{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

RELATED ARTICLES

Most Popular

Recent Comments