If you are puzzled about the use of swap case() in Python. Do not worry; we will show you how to use this function and provide some code examples to help you understand it better.
Python is an amazing language that comes with a lot of built-in features. One of these features is the swap case function. Swapcase() is a simple but powerful tool that you can use in your Python code to make text conversions between upper and lower case and perform other character conversions.
This tutorial will cover the basics of Python String swapcase() method and show you how to use them in your Python programs. So whether you are new to Python or a seasoned veteran, this article is sure to be interesting.
Table of Contents
What is Python swapcase( ) function?
The swapcase() function is a built-in function in the Python programming language that converts a string from one case to another (lower case to upper or vice versa). This can be useful when you have multiple strings that needs to be converted, and you do not want to write an explicit for loop every time.
Python String swapcase( )
Python swapcase() is a simple and easy to use library for performing case conversion on strings. It supports most of the common cases conversions, including uppercase to lowercase, lowercase to uppercase, and title case to camelCase. It’s also Unicode aware, so it can handle non-ASCII characters correctly.
There are different methods to swap strings in python mentioned below.
1. Python String Lowercase to Uppercase
Lowercase to uppercase conversion converts all letters in a word or phrase from their lowercase form to their uppercase form. This can be helpful when using text emails, online forms, or other general writing where proper capitalization is essential.
In the following code example, we have swapped the given lower case sentence into upper case. Let’s have a look.
# Declaring variable
s1 = "welcome to codeleaks"
s2 = s1.swapcase()
print (s2)
Output

2. Python String Uppercase to Lowercase
Python swapcase() function changes all uppercase characters to lowercase characters. When you call the swapcase function, it takes two String arguments. The first argument is the input String, and the second argument is the desired case for each character in that String.
To understand how to convert uppercase into lowercase , consider the code snippet below.
s1 = "WELCOME TO CODELEAKS"
s2 = s1.swapcase()
print (s2)
Output:

3. Python String Camelcase Conversion
The camelcase conversion is a simple rule that can help you convert upper- and lowercase letters into their camelcase equivalents. This process is often used when writing code or markup, as it makes the text easier to read and maintain. The Python camelcase() function does the same thing as the swapcase() function, but it converts the uppercase letters into lowercase letters and vice versa.
s1 = "Welcome to Codeleaks"
s2 = s1.swapcase()
print (s2)
Output

Also learn, how to convert string to integer.
FAQs
What are islower and isupper in Python?
If the entire string’s characters are in upper or lower case, the functions isupper() and islower() return True. These functions are specifically used to convert strings into upper or lower case.
Can we swap pointers in python?
In Python, objects are not variables; they are objects, and yes it is feasible to swap two pointers.
Conclusion
In this article, we discussed how the python string swapcase() method works, it allows you to convert strings from one case to the other without having to write an explicit loop. We also provided a few examples to demonstrate its use case. For example, if you want to know more about the other functions that are available in Python, do not forget to explore our blog.