Javaexercise.com

Python Program To Check Alphabet Character

In this section, we will create a program to check whether the specified character is an alphabet or not. We can check alphabet either by using user code or Python built-in function. Lets create some programs.

 

Python Program

In this program, we first take an input from the user then check it in the range of alphabets. If the character lies within range then it prints a message to the console.

# getting user input
ch = input("Enter any character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
    print(ch, "is an Alphabet")
else:
    print(ch, "is not an Alphabet")

 

Output:

Enter any character: s
s is an Alphabet

 

Python Program using Built-in function isalpha()

If we don't want to write any logical code to check alphabets then simply use Python provided built-in string method isalpha() that is used to check whether a string contains only alphabets or not. We can use it to check even for a single character. This method returns true if the specified string contains alphabets, false otherwise. 

# getting user input
ch = input("Enter a character or string: ")
# using string built-in method
val = ch.isalpha();
if val:
    print(ch, "is an Alphabet")
else:
    print(ch, "is not an Alphabet")

Output:

Enter a character or string: f
f is an Alphabet

 


Conclusion

Well, in this topic, we learnt to write a program to check whether the specified character is an alphabet or not. We wrote two programs: first by using the without any library function and second is using the python string isalpha() method.