The swapcase method
The swapcase method returns a string in which all characters of the string are in the opposite case to the original one. If the characters were in upper case, they will become lower case and vice versa. We do not specify anything in the method parameter.
Syntax
string.swapcase()
Example
Let's change the case of our string to the opposite:
txt = 'abcdef'
print(txt.swapcase())
Result of code execution:
'ABCDEF'
Example
Let's apply the swapcase method to the following line:
txt = 'AbcDef'
print(txt.swapcase())
Result of code execution:
'aBCdEF'
Example
Now let's apply the swapcase method to a string consisting of two words:
txt = 'AbWith DeA'
print(txt.swapcase())
Result of code execution:
'aBwith dEf'
See also
-
method
capitalize,
which changes the first character of a string to uppercase -
method
title,
which changes the first character of each word to uppercase -
method
upper,
which returns all characters of a string in uppercase -
method
lower,
which returns all lowercase ASCII characters -
method
islower,
which checks the characters of a string for lowercase -
method
istitle,
which checks the first character of a word for uppercase -
method
isupper,
which checks all characters in a string for uppercase