Python String swapcase()

Python String.swapcase() is used to swap the case of characters in given string. Lowercase characters are converted to uppercase, and the uppercase characters are converted to lowercase. swapcase() method returns a new resulting string after swapping cases, and the original string remains unchanged.

In this tutorial, we will learn the syntax and examples for swapcase() method of String class.

Syntax

The syntax to call swapcase() method on string x in Python is

</>
Copy
x.swapcase()

Examples

In the following program, we take a string 'Hello World', and swap the cases of the characters in this string using swapcase() method.

Example.py

</>
Copy
x = 'Hello World'
result = x.swapcase()
print('Original String  : ', x)
print('Result String    : ', result)

Output

Original String  :  Hello World
Result String    :  hELLO wORLD

Conclusion

In this Python Tutorial, we learned how to swap the cases of characters in a String using swapcase() method, with examples.