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.
- 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
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.
View all posts by Admin
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python String isalpha[] method is used to check whether all characters in the String is an alphabet.
Python String isalpha[] Method Syntax:
Syntax: string.isalpha[]
Parameters: isalpha[] does not take any parameters
Returns:
- True: If all characters in the string are alphabet.
- False: If the string contains 1 or more non-alphabets.
Errors and Exceptions:
- It contains no arguments, therefore an error occurs if a parameter is passed
- Both uppercase and lowercase alphabets return “True”
- Space is not considered to be the alphabet, therefore it returns “False”
Python String isalpha[] Method Example:
Python3
string
=
"geeks"
print
[string.isalpha[]]
Output:
True
Example 1: Working of isalpha[]
Python3
string
=
'Ayush'
print
[string.isalpha[]]
string
=
'Ayush0212'
print
[string.isalpha[]]
string
=
'Ayush Saxena'
print
[ string.isalpha[]]
Output:
True False False
Example 2: Practical Application
Given a string in Python, count the number of alphabets in the string and print the alphabets.
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush
Algorithm:
- Initialize a new string and variable counter to 0.
- Traverse the given string character by character up to its length, check if the character is an alphabet.
- If it is an alphabet, increment the counter by 1 and add it to a new string, else traverse to the next character.
- Print the value of the counter and the new string.
Python3
string
=
'Ayush Saxena'
count
=
0
newstring1
=
""
newstring2
=
""
for
a
in
string:
if
[a.isalpha[]]
=
=
True
:
count
+
=
1
newstring1
+
=
a
print
[count]
print
[newstring1]
string
=
'Ayush0212'
count
=
0
for
a
in
string:
if
[a.isalpha[]]
=
=
True
:
count
+
=
1
newstring2
+
=
a
print
[count]
print
[newstring2]
Output:
11 AyushSaxena 5 Ayush
Time Complexity: O[n]
Auxiliary Space: O[n]