Swap the case using swapcase() Function in Python

swapcase() Function in python swaps the case of the every single character of the string from upper case to lower case and from lower case to upper case.

  • If the input string is in small case it swaps the result to upper case.
  • If the input string is in upper case it swaps the result to small case.
  • If input string is in mix of small case and upper case then it swaps all the upper case character to small case and all the small case character to upper case.

 

Syntax of swapcase() Function in Python:

str.swapcase();

Example of swapcase() Function in Python:

str1 = "Salute to the Mother Earth"
str2= "Jupiter is the largest planet"
str3= "MERCURY IS CLOSE TO SUN"

print str1.swapcase()
print str2.swapcase()
print str3.swapcase()

swap function simply swaps each and every character in the string so the output will be

sALUTE TO THE mOTHER eARTH
JUPITER IS THE LARGEST PLANET
mercury is close to sun

 

previous swapcase() Function in Python                                                                                                                next swapcase() Function in Python