The swapcase method in Python is used to change the case of each character in a string, transforming uppercase letters to lowercase and vice versa. This method does not take any arguments and will return a new string that reflects the case transformation.
Signature
swapcase()
Parameters
No Parameter
Return
It returns a string.
Let us examine a few instances of the swapcase method to grasp its functionality.
Python String swapcase Method Example 1
A straightforward illustration of changing uppercase letters to lowercase can be accomplished through the utilization of the swapcase method.
# Python String swapcase() method
# Declaring variable
str = "HELLO LOGICPRACTICE"
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
hello logicpractice
Python String swapcase Method Example 2
This illustration demonstrates the process of transforming lowercase letters into their uppercase equivalents.
# Python String swapcase() method
# Declaring variable
str = "hello logicpractice"
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
HELLO LOGICPRACTICE
Python String swapcase Method Example 3
When the input string is formatted in camelcase, the result produced by this method will also adhere to the camelcase format. Every lowercase letter will be transformed into its uppercase counterpart, while all uppercase letters will be converted to lowercase.
# Python String swapcase() method
# Declaring variable
str = "Hello World"
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
hELLO hELLO wORLD