Python string Method - swapcase()
The swapcase()
method is a built-in string method in Python that returns a copy of a string with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
The syntax for using swapcase()
method is as follows:
string.swapcase()
Here, string
is the original string.
The method returns a new string with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
Here's an example of using the swapcase()
method:
string = "Hello, World!" swapped_string = string.swapcase() print(swapped_string)
Output:
hELLO, wORLD!
In the example above, the swapcase()
method was used to convert the original string "Hello, World!"
to a new string with uppercase and lowercase characters swapped. The resulting string is stored in the variable swapped_string
. Note that uppercase characters are converted to lowercase and lowercase characters are converted to uppercase.