Method 1
Using the built-in functions in Pythonisupper()
cap (a poem)islower()
to determine whether a letter is uppercase or lowercase.
# Get user input letter = input("Please enter one letter:") # Determine if a letter is uppercase if (): print("The letter is capitalized.") # Determine if a letter is lowercase elif (): print("The letter is lowercase.") # Outputs an error message if it is neither uppercase nor lowercase else: print("Input error, please enter a letter.")
The user enters a letter, and the program uses the isupper() and islower() functions to determine whether the letter is uppercase or lowercase and outputs the appropriate message. If the user enters a character other than a letter, the program outputs an error message.
Method II
Use ASCII values to determine letter case.
# Get user input letter = input("Please enter one letter:") # Determine if a letter is uppercase if ord(letter) >= 65 and ord(letter) <= 90: print("The letter is capitalized.") # Determine if a letter is lowercase elif ord(letter) >= 97 and ord(letter) <= 122: print("The letter is lowercase.") # Outputs an error message if it is neither uppercase nor lowercase else: print("Input error, please enter a letter.")
ASCII values are the standard coding system used to represent text characters, with each character corresponding to a unique ASCII value. For the English alphabet, ASCII values range from 65 to 90 for uppercase letters and 97 to 122 for lowercase letters.
Using Python's built-in functionsord()
Converts the input letters to their corresponding ASCII values and uses conditional statements to determine the case of the letters.
Method III
Use the alphabet in Python.
# Define the alphabet uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" lowercase_letters = "abcdefghijklmnopqrstuvwxyz" # Get user input char = input("Please enter one letter:") # Determine if a character is uppercase if char in uppercase_letters: print("The letters entered are in uppercase.") # Determine if a character is lowercase elif char in lowercase_letters: print("The letters entered are in lower case.") # Outputs an error message if the character entered is not a letter else: print("The entry was not alphabetic. Please re-enter.")
The program defines a string containing uppercase and lowercase letters and then uses thein
operator to determine whether the input character is included in the string. If the input character is an uppercase letter, the corresponding message is output; if it is a lowercase letter, the corresponding message is output; otherwise, an error message is output.
Method IV
Use regular expressions.
# Import re modules import re # Get user input letter = input("Please enter one letter:") # Determine if a letter is uppercase if (r'[A-Z]', letter): print("The letter is capitalized.") # Determine if a letter is lowercase elif (r'[a-z]', letter): print("The letter is lowercase.") # Outputs an error message if it is neither uppercase nor lowercase else: print("Input error, please enter a letter.")
utilization()
function and regular expressions to match the case of letters. If the letter is uppercase, it matches strings that begin with an uppercase letter; if the letter is lowercase, it matches strings that begin with a lowercase letter.
python upper and lower case letter swap
Interchanging upper and lower case letters can be accomplished in the Python language using the string (string) method. String is often one of the most commonly used types of variables in the Python language, and its methods can be used to manipulate strings and implement various string operations, and the conversion of upper and lower case letters can also be achieved using the string method. Here is one way to implement upper and lower case letter swapping:
1. Use .upper() to convert lowercase letters in a string to uppercase letters.
2. Use .lower() to convert uppercase letters in a string to lowercase letters.
3. Use .swapcase () to convert uppercase letters in a string to lowercase letters, while converting lowercase letters to uppercase letters.
For example, the following code converts lowercase letters in a string to uppercase letters:
str = "hello world" str_upper = () print(str_upper)
The result is: "HELLO WORLD".
Also, the following code converts uppercase letters in a string to lowercase letters:
str = "HELLO WORLD" str_lower = () print(str_lower)
The result is: "hello world".
Finally, the following code converts lowercase letters in a string to uppercase and uppercase letters to lowercase:
str = "Hello World" str_swap = () print(str_swap)
The result of the run is: "hELLO wORLD"
summarize
to this article on the use of python to determine the letter case of several methods of summary of the article is introduced to this, more related python to determine the letter case content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!