Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to check the given character is alphabet or digit or special character.
Here’s a Python program that will check whether a given character is an alphabet, a digit, or a special character:
Table of Contents
Code for python program to check the given character is alphabet or digit or special character
def check_char(c):
if c.isalpha():
print(f"{c} is an alphabet.")
elif c.isdigit():
print(f"{c} is a digit.")
else:
print(f"{c} is a special character.")
# Test the function
check_char('a')
check_char('1')
check_char('$')
Output
a is an alphabet.
1 is a digit.
$ is a special character.
Explanation
This program uses the isalpha()
and isdigit()
methods to check whether a character is an alphabet or a digit, respectively. If the character is neither an alphabet nor a digit, it is assumed to be a special character.
You can test this program by calling the check_char
function with any character as the argument. The function will print out whether the character is an alphabet, a digit, or a special character.
Python program to check the given character is alphabet or digit or special character
Conclusion
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.