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
Below we have a basic syntax of String
swapcase() in Python:
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
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.
swapcase(): Returned Values
For the returned value there are two cases:
swapcase(): Basic Example
Below we have an example to show the working of String
h1= “Hello StudyTonight”
The Output for the same is given below; always remember this method does not take any parameters:
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:
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.