Python string swapcase() Method (original) (raw)

Last Updated : 02 Jan, 2025

swapcase() method in Python is used to return a new string where all the uppercase characters in the original string are converted to lowercase, and all the lowercase characters are converted to uppercase.

Let’s us understand this with the help of an example:

Python `

s = "I love Python"

swapped_text = s.swapcase() print(swapped_text)

`

Explanation:

Table of Content

Syntax of String swapcase() method

string.swapcase()

Parameters

Return Type

Simple toggle case

Let’s see how sswapcase() works with a mixed-case string:

Python `

s = "Python IS Awesome!"

res = s.swapcase() print(res)

`

Explanation:

Handling numeric and special characters

The swapcase()method does not affect numeric or special characters:

Python `

s = "123 Python!"

res = s.swapcase() print(res)

`

Explanation:

Applying swapcase() to an all-uppercase string

What happens if the string contains only uppercase characters?

Python `

s = "HELLO" res = s.swapcase() print(res)

`

Explanation:

4. Applying swapcase() to an all-lowercase string

Similarly, let’s see the result with an all-lowercase string:

Python `

s = "world" res = s.swapcase() print(res)

`

Explanation: