Python program to swap case of English word


Suppose we have a string with English letters. We have to swap the case of the letters. So uppercase will be converted to lower and lowercase converted to upper.

So, if the input is like s = "PrograMMinG", then the output will be pROGRAmmINg

To solve this, we will follow these steps −

  • ret := blank string
  • for each letter in s, do
    • if letter is in uppercase, then
      • ret := ret concatenate lower case equivalent of letter
    • otherwise,
      • ret := ret concatenate upper case equivalent of letter
  • return ret

Example

Let us see the following implementation to get better understanding

def solve(s):
   ret = ''

   for letter in s:
      if letter.isupper():
         ret += letter.lower()
      else:
         ret += letter.upper()
   return ret

s = "PrograMMinG"
print(solve(s))

Input

"PrograMMinG"

Output

pROGRAmmINg

Updated on: 11-Oct-2021

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements