Python String swapcase()

The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.

The format of swapcase() method is:

string.swapcase()

Note: Not necessarily, string.swapcase().swapcase() == string


String swapcase() Parameters()

The swapcase() method doesn't take any parameters.


Return value from String swapcase()

The swapcase() method returns the string where all uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase.


Example 1: Swap lowercase to uppercase and vice versa using swapcase()

# example string
string = "THIS SHOULD ALL BE LOWERCASE."
print(string.swapcase())

string = "this should all be uppercase."
print(string.swapcase())

string = "ThIs ShOuLd Be MiXeD cAsEd."
print(string.swapcase())

When you run the program, the output will be:

this should all be lowercase.
THIS SHOULD ALL BE UPPERCASE.
tHiS sHoUlD bE mIxEd CaSeD.

Note: If you want to convert string to lowercase only, use lower(). Likewise, if you want to convert string to uppercase only, use upper().