How to Convert String to Lowercase or Uppercase in Python
Sometimes it is necessary to convert strings with mixed casing into lower or upper case. In this tutorial, we will learn how to do both in Python.
Convert String to Lowercase
To convert all characters into lower case, use the Python .lower()
function and pass the string in front of the .
(dot).
string = 'mIxEd Case sTrInG'
lwr_case = string.lower()
print(lwr_case)
print(string)
mixed case string
mIxEd Case sTrInG
.lower()
does not modify the original variable, so you will need to store the result from the function in a new one.
Convert String to Uppercase
To convert all characters to uppercase, use the Python .upper()
function and pass the string before the .
(dot).
string = 'mIxEd Case sTrInG'
upr_case = string.upper()
print(upr_case)
print(string)
As with .lower()
you will also need to store the result from .upper()
in a variable as the original string will not be modified.
string