Search for Courses, Topics
certificate icon
Certificate

Learn on Scaler Topics and get certified.

static-certificate

swapcase() in Python

Learn about swapcase() function in Python.

17 Feb 2022-4 mins read

Overview

The swapcase() method of str class converts uppercase alphabetic characters of a string to lowercase and lowercase alphabetic characters of the same string to uppercase.

Syntax for swapcase() in Python

The syntax of swapcase function in Python is:

string.swapcase()

where string is the name of the string on which swapcase is performed.

Parameters for Swapcase in Python

The swapcase method in Python doesn't take any parameters.

Return Values for swapcase() in Python

Return Type: str

swapcase() in Python returns a string where all uppercase alphabetic characters are converted to lowercase, and lowercase alphabetic characters are converted to uppercase. Special characters and numbers remain unchanged.

Example of swapcase() in Python

my_string = "foLLow SCALER topics on TWIttER"

# Swapping the case with the swapcase function
my_string_swapcase = my_string.swapcase()

print(my_string_swapcase)

Output of swapcase in Python

FOllOW scaler TOPICS ON twiTTer

What is swapcase() Function in Python?

Python's str class provides several methods to perform some common operations on a string.

One of such method is swapcase(). Swapcase method is used for converting uppercase letters of a string to lowercase and lowercase letters of a string to uppercase.

Note:

  • The swapcase function does not modify the original string, it only returns a new string created by swapping the cases.
  • Numbers and special characters are not modified.
  • It is not necessarily true that s.swapcase().swapcase() == s.
    • There are some unicode lower case letters such as the micro character µ (U+00B5) and the mu character μ (U+03BC) which have the same uppercase couterparts that is u'\u039c'. In that case doing u'\xb5'.swapcase().swapcase() will not be true. Because u'\xb5 will be changed to u'\u039c' on first application of swapcase and then u'\u039c' will be changed to u'\u03bc' on second application of swapcase.
    • Another example of such case is with the German consonant ß. It becomes SS after one application of swapcase and then ss after the second. See "More Examples" section for code and details.

More Examples

Example 1: Swap lowercase to uppercase and vice versa using swapcase() in Python

my_string = "This STRING iS miXed CAse"

print(my_string.swapcase())

Output:

tHIS string Is MIxED caSE

Explanation:

This is the usual behaviour of swapcase function in Python as discussed before, it will transform lowercase to uppercase and vice versa.

Example 2: Using swapcase() with a string containing special characters

my_string = "THIS string contains NUMBERS and SPECIAL characters: $12345@6789#"

print(my_string.swapcase())

Output:

this STRING CONTAINS numbers AND special CHARACTERS: $12345@6789#

Explanation:

The swapcase method does not affect any number or special character when transforming a string. In the example, we have a string that contains $12345@6789#, this remains unchanged after the applying swapcase() function and rest of the letters are modified.

Example 3: Not necessarily, s.swapcase().swapcase() == s

'ß'.swapcase().swapcase()

Output:

ss

Explanation:

Why ss? Because the German consonant ß has been assigned SS as its uppercase counterpart. Thus, 'ß'.swapcase() returns SS which is then converted to ss on second application of swapcase.

Example 4: Real-world Use of swapcase() in Python

text = input("Enter text: ")

text_fixed = text.swapcase()

print(text_fixed)

Output:

Enter text: i FORGOT TO TURN OFF MY capslock!!
I forgot to turn off my CAPSLOCK!!

Explanation:

Suppose you are creating a tool that helps people who forgot to turn off their capslock before writing. That's why they wrote uppercase letters by default and lowercase letters when they pressed Ctrl + shift + key. Now they need to fix the text by converting lowercase to uppercase and vice-versa.
In this example, We took the text as input and used swapcase to return the desired text.

Conclusion

  • The swapcase() method of str class converts uppercase characters to lowercase and lowercase characters to uppercase.
  • Swapcase function does not modify the original string, it returns a new string.
  • Swapcase function does not affect numbers and special characters of a string.