Exercise
Function To Verify Alphabetic Characters
Objective
Develop a Python program that includes a function to determine if a given character is alphabetic (from A to Z). The function can be used to check if a character like 'a' is alphabetic, and if so, the program will print a message saying 'It is an alphabetic character'. Remember, you don't need to consider accented characters or 'ñ'.
Example Python Exercise
Show Python Code
# Function to check if a given character is alphabetic (from A to Z)
def is_alphabetic(char):
# Check if the character is alphabetic (between 'a' and 'z' or 'A' and 'Z')
if char.isalpha() and len(char) == 1:
return True
else:
return False
# Example usage of the is_alphabetic function
def main():
# Take a character input from the user
char = input("Enter a character: ")
# Check if the input character is alphabetic
if is_alphabetic(char):
print(f"It is an alphabetic character.")
else:
print(f"It is not an alphabetic character.")
# Run the main function if the script is executed directly
if __name__ == "__main__":
main()
Output
Enter a character: a
It is an alphabetic character.
Enter a character: 1
It is not an alphabetic character.
Share this Python Exercise