swapcase() in Python
Learn about swapcase() function in Python.
Swapcase in Python
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:
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
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.
- 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.
Example 1: Swap lowercase to uppercase and vice versa using swapcase()
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
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
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()
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.
- 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.