Python Convert String to Uppercase or Lowercase

Document 对象参考手册Python3 Examples

In Python, you can useupper()andlower()methods to convert a string to uppercase or lowercase.upper()The method converts all characters in the string to uppercase, whilelower()The method converts all characters in the string to lowercase.

Example

# Original string
original_string = "Hello, World!"

# Convert to uppercase
upper_case_string = original_string.upper()

# Convert to lowercase
lower_case_string = original_string.lower()

# Output result
print("Original String:", original_string)
print("Upper Case String:", upper_case_string)
print("Lower Case String:", lower_case_string)

Code explanation:

  • original_string.upper(): Callupper()method to convert the stringoriginal_stringAll characters in are converted to uppercase, and the result is stored inupper_case_string.
  • original_string.lower(): Calllower()method to convert the stringoriginal_stringAll characters in are converted to lowercase, and the result is stored inlower_case_string.
  • print(): Used to output the original string, the uppercase-converted string, and the lowercase-converted string.

Output result:

Original String: Hello, World!
Upper Case String: HELLO, WORLD!
Lower Case String: hello, world!

Document 对象参考手册Python3 Examples

Other Extensions