Python String swapcase() method
Python String swapcase() method
The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa.
Syntax
string.swapcase()
Parameter
NA
Return
This method returns a copy of the string with uppercase characters converted to lowercase and vice versa.
Example 1
# Python program explaining
# the string.swapcase() method
string="heLLo wORLd"
# observe the original string
print("Actual String: ",string)
# swaping the string i.e upper case to lower case or vice versa
val = string.swapcase()
# printing the swap cased string
print("Uppercased string: ",val)
Output
Actual String: heLLo wORLd Uppercased string: HEllO WorlD
Example 2
# Python program explaining
# the string.swapcase() method
string1="heLLo wORLd"
# observe the original string
print ('Original String: ', string1 )
print ('Converted String is = ', string1.swapcase())
# passing special characters
string2 = 'mY#namE#Is#REEma#panDA'.swapcase()
print ('\nSecond Output for swapcase() method is = ', string2 )
# passing integer values
string3 = '98041'.swapcase()
print('\nFourth Output after swapcase() method is = ', string3 )
string4 = 'pyTHon\niS\neAsy\ntO\nLEARN'.swapcase()
print ('\nThird Output after swapcase() method is = ', string4)
Output
Original String: heLLo wORLd Converted String is = HEllO WorlD Second Output for swapcase() method is = My#NAMe#iS#reeMA#PANda Fourth Output after swapcase() method is = 98041 Third Output after swapcase() method is = PYthON Is EaSY To learn
