The string method swapcase() used to switch the case of letters in the string. It converts uppercase letters into lowercase and lowercase to uppercase. It returns string after switching case of letters. It ignores numbers, symbols. It also swap case for locale specific characters.
string : A string to switch case of letters
1 string1 = "Welcome To Python 3.1 !"
2 string2 = "Welcome to IOGyan"
3
4 r1 = string1.swapcase()
5 r2 = string2.swapcase()
6
7 print('A resulting string :', r1)
8 print('A resulting string :', r2)
In the above example, a string variables are define with number, symbol and locale specific characters. A swapcase() method called for string variable which returns string by swapping lowercase into uppercase and vice versa. The result is stored in the variable that will be printed on console.
1 A resulting string : wELCOME tO pYTHON 3.1 !
2 A resulting string : wELCOME TO iogYAN
swapcase() with different locale
swapcase() with different locale
1 string1 = "müller !"
2 string2 = "Straße"
3
4 r1 = string1.swapcase()
5 r2 = string2.swapcase()
6
7 print('A resulting string :', r1)
8 print('A resulting string :', r2)
In the above example, a string variables are define and initialized with string literals with different locale. A swapcase() method called that convert string by swapping case respective locale case and returns a resulting string.
1 A resulting string : MÜLLER !
2 A resulting string : sTRASSE
Related options for your search