What is swapcase() function in the Python?

It is a string’s function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.

Example:

  1. string = “IT IS IN LOWERCASE.”  
  2. print(string.swapcase())  
  3.   
  4. string = “it is in uppercase.”  
  5. print(string.swapcase())  

Output:

it is in lowercase. 
IT IS IN UPPERCASE.