There are many ways for Python to judge the case of letters, such as judging by built-in functions, judging by ASCII code values, or judging by regular expressions. Next, let's explain the specific usage methods.
Method 1. Use the built-in functions isupper() and islower() in Python to determine whether a letter is an uppercase or lowercase letter.
# get user input
letter = input("Please enter a letter:")
# Determine whether the letter is uppercase
if letter.isupper():
print("The letter is uppercase.")
# Determine whether the letter is lowercase
elif letter.islower():
print("The letter is a lowercase letter.")
# If it is neither uppercase nor lowercase, output an error message
else:
print("Input error, please enter a letter.")
The user inputs a letter, and the program uses the isupper() and islower() functions to determine whether the letter is uppercase or lowercase, and outputs the corresponding information. If the user enters a character other than letters, the program will output an error message.
Method 2. Use the ASCII code value to determine the case of letters
# get user input
letter = input("Please enter a letter:")
# Determine whether the letter is uppercase
if ord(letter)>= 65 and ord(letter) <=90:
print("The letter is uppercase.")
# Determine whether the letter is lowercase
elif ord(letter) >= 97 and ord(letter) <=122:
print("The letter is a lowercase letter.")
# If it is neither uppercase nor lowercase, output an error message
else:
print("Input error, please enter a letter.")
Each character has a unique ASCII code value, which is the standard encoding system used to represent text characters. The ASCII code values of English letters range from 65 to 90 for uppercase letters, and 97 to 122 for lowercase letters.
Use Python's built-in function ord() to convert the input letters into corresponding ASCII code values, and use conditional statements to determine the case of letters.
Method 3. Use the alphabet in Python.
# define the alphabet
uppercase_letters = ""ABCDEFGHIJKLMNOPQRSTUVWXYZ""
lowercase_letters = ""abcdefghijklmnopqrstuvwxyz""
# get user input
char = input("Please enter a letter:")
# Determine if a character is an uppercase letter
if char in uppercase_letters:
print("Entered letters are capital letters")
# Determine if the character is a lowercase letter
elif char in lowercase_letters:
print("The input letters are lowercase letters")
# If the input character is not a letter, output an error message
else:
print("The input is not a letter, please re-enter")
The program defines a string containing uppercase and lowercase letters, and then uses the in operator to determine whether the entered character is contained in the string. If the input character is an uppercase letter, then output the corresponding information; if it is a lowercase letter, then output the corresponding information; otherwise, output an error message.
Method 4, using regular expressions
# import re module
improt re
# get user input
letter = input("Please enter a letter:")
# Determine whether the letter is uppercase
if re.match(r'[A-Z]',letter):
print("The letter is uppercase.")
# Determine whether the letter is lowercase
elif re.match(r'[a-z]',letter):
print("The letter is a lowercase letter.")
# If it is neither uppercase nor lowercase, output an error message
else:
print("Input error message, please enter a letter.")
Use the re.match() function and regular expressions to match the case of letters. Matches strings starting with an uppercase letter if the letter is uppercase; matches a string starting with a lowercase letter if the letter is lowercase.