Introduction
Hello Reader!!
Python, one of the most convenient languages, has various built-in string functions. One such function is the swapcase() function. In this article, we will learn about the swapcase() in Python function, how to use it, and its alternatives with the help of examples.

So, let’s get started!
What is Swapcase() Function in Python?
There are two parallel sets of letters used in uppercase and lowercase writing systems, and each letter in one set often has an equivalent in the other. Lowercase letters are smaller and shorter forms of letters (such as a), in contrast to uppercase letters, which are larger and taller (such as A). swapcase() in python is a very helpful method for changing the case of letters in a string in Python. Python swapcase() provides a replica of the string with all case-sensitive characters swapped out.
Python swapcase() definition- This is a built-in string function that returns a duplicate of the specified input string by converting all uppercase characters to lowercase and all lowercase characters to uppercase.
Important points
- The function of swapcase() in python does not change the original string; instead, it returns a new string generated by changing the cases.
- No modifications are made to numbers or special characters.
- It is not always the case that str.swapcase().swapcase() == str.
Using swapcase() to handle strings in python
Let us now see various uses of the swapcase() function in python to handle strings:
Syntax for Swapcase() in Python
Let “str” be the name of the string on which swapcase() is performed. Then, the syntax is:
str.swapcase()
Parameters for Swapcase() in Python
The function of swapcase() in python does not take any parameters.
The Python interpreter will return a TypeError exception if any parameters are passed to the function.
Return Value for Swapcase() in Python
The swapcase() function returns a string in which all uppercase characters are converted to lowercase, and all lowercase characters are converted to uppercase.
Examples for Swapcase() in Python
Example1: Swapping lowercase to uppercase and uppercase to lowercase using swapcase() in python
str = 'lower case to upper case using swapcase'
print('String provided:', str)
print('String returned:', str.swapcase())
print('')
str = 'UPPER CASE TO LOWER CASE USING SWAPCASE'
print('String provided:', str)
print('String returned:', str.swapcase())
print('')
str = 'mIxEd CasE UsING SwapCASe'
print('String provided:', str)
print('String returned:', str.swapcase())
Output
String provided: lower case to upper case using swapcase
String returned: LOWER CASE TO UPPER CASE USING SWAPCASE
String provided: UPPER CASE TO LOWER CASE USING SWAPCASE
String returned: upper case to lower case using swapcase
String provided: mIxEd CasE UsING SwapCASe
String returned: MiXeD cASe uSing sWAPcasE
Example 2: swapcase() with string containing special characters
If the input string includes characters of various types, such as numerals, signs, symbols, and so on. The function of swapcase() in python ignores such letters and converts alphabetic characters to their corresponding opposite cases.
txt = 'CoDinG NinJAS 123456'
print('String provided:', txt)
print('String returned:', txt.swapcase())
print('')
txt = '@!#^&($%* Python Is FUn wITh CoDinG NinJas'
print('String provided:', txt)
print('String returned:', txt.swapcase())
Output
String provided: CoDinG NinJAS 123456
String returned: cOdINg nINjas 123456
String provided: @!#^&($%* Python Is FUn wITh CoDinG NinJas
String returned: @!#^&($%* pYTHON iS fuN WitH cOdINg nINjAS
Alternative of swapCase() in Python
Python has a variety of string methods, and only a couple can handle operations equivalent (but not exact) to the swapcase() in python function. Some of these functions include isupper(), islower(), isspace(), upper(), and lower(). In the above example of mixed string case, let us now use these functions to get the same results.
Example of Alternative of swapCase() in Python
str = 'mIxEd CasE UsING SwapCASe'
new_str = ''
for s in str:
# check lowercase characters and convert to uppercase
if(s.isupper()) == True:
new_str += (s.lower())
# check uppercase characters and convert to lowercase
elif (s.islower()) == True:
new_str += (s.upper())
# check whitespace and add a new string
elif (s.isspace()) == True:
new_str += s
print('Original String:', str)
print('Changed String:', new_str)
Output
Original String: mIxEd CasE UsING SwapCASe
Changed String: MiXeD cASe uSing sWAPcasE
Frequently Asked Questions
What does Swapcase () do in Python?
The swapcase() in python function returns a string in which all upper case letters are converted to lower case and vice versa.
Is Swapcase a built-in function in Python?
The function of swapcase() in python is a built-in string function that returns a string by converting all uppercase to lowercase letters and vice versa.
How do you reverse the case of a word in Python?
The swapcase() method returns a string where all the upper case letters are lower case and vice versa.
How do you convert all uppercase to lowercase in Python?
Python built-in lower() method is used to convert all uppercase to lowercase in Python. It returns a string that has been converted from all uppercase to all lowercase characters.
Conclusion
In this article, we learned about the swapcase() in python method. We also learned to use it and its alternative methods with the help of various examples.
We hope this article has clarified your understanding of the String built-in functions in python. You can refer to our blogs to understand more about built-in functions in python.
- Functions in Python - Coding Ninjas Coding Ninjas Studio
- Python Built-in Functions - Coding Ninjas Coding Ninjas Studio
- Python | Learn & Practice from Coding Ninjas Studio
- Fibonacci Series in Python
- Python String Concatenation
You can also visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can prepare for interviews with interview experiences and an interview bundle.
Keep learning and keep growing, Ninjas!