Signup/Sign In
LAST UPDATED: JUNE 4, 2020

Python String swapcase()

In Python, String swapcase() is an inbuilt method which is used for string handling.

  • As from the name it is clear that here is swapping of the case which in turn means that this method is used to swap the case of a string.

  • This method converts the string which is in Uppercase into Lowercase case and vice -versa.

  • Suppose a string is a="ABC" is given and if swapcase() method is applied on to it then result will be abc.

Python String swapcase(): Syntax

Below we have a basic syntax of String swapcase() in Python:

string.swapcase()

Note: In the above syntax; string denotes the string value that holds the input value whose case is needed to be changed or to which we will apply swapcase() method.

Python String swapcase(): Parameters

From the syntax of this method, it is clearly seen that there is no parameter in this method.

  • If we pass any parameter to this method then this method will raise an error.

Python String swapcase(): Returned Values

For the returned value there are two cases:

  • When This method is applied to Uppercase values then the result will be in Lowercase.

  • When this method is applied to Lowercase values then the result will be in Uppercase.

Python String swapcase(): Basic Example

Below we have an example to show the working of String swapcase() function:

h1= “Hello StudyTonight”
h1.swapcase()

The Output for the same is given below; always remember this method does not take any parameters:


hELLO sTUDYtONIGHT

String swapcase(): Another Example

a = "joHn Cena"
b = "Hello StudyTonight"
c = "Qwerty"


print("Original String: ", a, "Case changed: ", a.swapcase())
print("Original String: ", b, "Case changed: ", b.swapcase())
print("Original String: ", c, "Case changed: ", c.swapcase())

In the above example, we print the values of the original string as well result after swapcase() is applied to them. The Output for the same is given below:


Original String: joHn Cena Case changed: JOhN cENA
Original String: Hello StudyTonight Case changed: hELLO sTUDY tONIGHT
Original String: Qwerty Case changed: qWERTY

Time For Live Example!

Now its time to see a Live example of swapcase() method which is given below:

Summary

In this tutorial, we have learned the swapcase() method of strings in Python with the help of which we can convert an uppercase character into lowercase and vice-versa. We also discussed its parameters and returned values followed by a Live example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.