Python swap case of each character of a string

Python swap case of each character of a string :

This tutorial will show you how to swap case of each character of a string. For example, if the string is Hello, it will change it to hELLO.

The first thing that comes to our mind for solving these types of questions is to use a loop or iterate through the elements. We can think about solving this problem by iterating through the string character by character. But the main problem is that string is immutable in python, i.e. we can’t modify any character of a string directly. We can iterate through the characters of the string one by one but we can’t change any of them directly.

The Python way :

Thankfully, python comes a lot of useful built-in methods for saving our precious time. No need to write a new method, just use it and done.

This inbuilt method that Python provides us to swap all characters of a string is called swapcase(). It_ swaps the uppercase characters to lowercase, lowercase characters to uppercase_ in a string and returns the string.

No external module is required to import for using this method. You will get it right out of the box. Anywhere you want to change the character cases of a string, just call it. Let’s take a look at the definition of swapcase :

Definition :

The swapcase method is defined as below :


It doesn’t take any parameter. Use it on any string and get the modified string as the return string.

One more thing to note that not always s.swapcase().swapcase() == s for string s. This is because the first_ ’swapcase()’_ will change the case of each character of the original string and the second_ ‘swapcase()’_ will change them back as the original.

For example, for the string_ ‘Hello’,_ the first ‘swapcase()’ will change it to ‘hELLO’ and the second ‘swapcase()’ will change it to ‘Hello’ back again.

But, the new string is not the same as the original, or the new string object is not equal to the original string. The value of_ ’s.swapcase().swapcase()_ is s’ is False because a new string will be generated whenever we are calling swapcase() and the last string will be a different one. It looks like the same as the original string but it is actually created using the first swapcase() method.

Example :

Now let’s try to learn how swapcase() works with an example :

python swap case of character

python swapcase example

As you can see that only the alphabets are changed, not the numbers and any other special characters.

Using swapcase() two times :

python swapcase characters

python swapcase example

As you can see that the last comparison returns False. Similar to ϕ, there are lots of other characters that will return false for swapcase().

Another way to solve this problem :

If you are solving this problem in an interview and the interviewer asked you to solve it with a different way, how will you do it?

A string is immutable and we can’t modify it. But we can iterate through the characters of the string.

  1. Iterate through the characters of the string one by one using a for loop like ‘for c in string:’.
  2. Change the case of each character. We can check if a character is lowercase by using the ‘islower()’ method and convert it to an upper case using ‘upper()’ method. Similarly, we have ‘isupper()’ and ‘lower()’ methods available to check if a character is uppercase or not and to change it to a lower case. Using a simple ‘if-else’ condition, we can implement this step.
  3. After the case is changed, add each character to a list. Keep adding all characters until the scanning is completed.
  4. Finally, convert the list to a string by using ‘join’, ‘.join(list)’ and print out the result.

Try to implement the above program and drop a comment below with the code if it is working.

You might also like :