Python String Swapcase Method

In python, the string swapcase() method is useful to convert the given string lowercase characters to uppercase and uppercase characters to lowercase.

 

Following is the pictorial representation of the string swapcase() method functionality in python.

 

Python string swapcase method example result

 

If you observe the above diagram, we are trying to convert the given string “Hi GUEST Hi” lowercase characters to uppercase and uppercase characters to lowercase using the swapcase function in python.

String Swapcase Method Syntax

Following is the syntax of defining a string swapcase method in python.

 

string.swapcase()

The python swapcase() method will not accept any parameters.

String Swapcase Method Example

Following is the example of converting the given string lowercase characters to uppercase and uppercase characters to lowercase using the string swapcase() method in python.

 

msg1 = "Hi guest Hi"
print(msg1.swapcase())
msg2 = "welcome to tutlane"
print(msg2.swapcase())
msg3 = "LEARN PYTHON"
print(msg3.swapcase())

If you observe the above example, we are converting the given string lowercase characters to uppercase and uppercase characters to lowercase using the swapcase() method.

 

When you execute the above python program, you will get the result as shown below.

 

hI GUEST hI
WELCOME TO TUTLANE
learn python

This is how you can use the string swapcase() method in python to swap the given string characters based on your requirements.