Here are we are going to see how to do python string case conversion.

Python String Case Conversion:

Python provides the following methods to perform the string case conversion to produce a new string, that is lower case to upper case and upper case to lower case.

  • str.lower() # Produces all lowercase string
  • str.upper() # Produces all uppercase string
  • str.title() # ‘python string’.title() => ‘Python String’
  • str.swapcase() # Upper to lower, and vice versa

The above all methods reruns a new string after conversion. The original string will be still available.

str.lower():

The str.lower() method produces all lowercase string.

str = "Hello Every One" 
new_str = lower() 
print(new_str)

Output:

hello every one

str.upper():

The str.upper() method produces all uppercase string.

str = "Hello Every One"
new_str = str.upper()
print(new_str)

Output:

HELLO EVERY ONE

str.title():

The title() method produces the title case of the string that means converts the every word’s first letter as capital case. If the string contains apostrophes (') then it also converts the letter to upper after the apostrophes.

str = "python is every one's fav language"
new_str = str.title()
print(new_str)

Output:

Python Is Every One'S Fav Language

str.swapcase():

The str.swapcase() is really used method, the string it produces has an uppercase letter where the source string had a lowercase latter, and vice versa.

str = "Python Is My Fav Programming Language"
new_str = str.swapcase()
print(new_str)

Output:

pYTHON iS mY fAV pROGRAMMING lANGUAGE

References:

Happy Learning 🙂