Python Convert a String to Uppercase or Lowercase
In Python, you can use the upper() and lower() method to convert a string to uppercase or lowercase.upper() The method converts all characters in the string to uppercase, while the lower() method converts all characters in the string to lowercase.
Example
Raw strings
original_string
=
"Hello, World!"
Convert to uppercase
upper_case_string
=
original_string.
upper
(
)
Convert to lowercase
lower_case_string
=
original_string.
lower
(
)
Output result
(
"Original String:"
,
original_string
)
(
"Upper Case String:"
,
upper_case_string
)
(
"Lower Case String:"
,
lower_case_string
)
Code analysis:
original_string.upper(): Callingupper()The method splits the stringoriginal_stringConvert all characters in to uppercase and store the result inupper_case_stringit.original_string.lower(): Callinglower()The method splits the stringoriginal_stringConvert all characters in to lowercase and store the result inlower_case_stringit.print(): Print the original string, the uppercase string and the lowercase string.
Output result:
Original String: Hello, World!
Upper Case String: HELLO, WORLD!
Lower Case String: hello, world!
Python3 Examples