String swapcase() | Swap the letter cases of a given string in Python

4.8/5 - (6 votes)

Problem

Swap case a string. Convert all lowercase letters to uppercase letters and vice versa.

Solution 1 – Using built-in swapcase() string method

Read here all Python String methods.

s = input('Enter text here: ')
print(s.swapcase())

or

Solution 2 – avoids using the built in swapcase() function

s = input('Enter text: ')

def swap_case(s):
    result = ""
    for letter in s:
        if letter == letter.upper():
            result += letter.lower()
        else:
            result += letter.upper()
    return result

print(swap_case(s))

//
Enter text: WelCome TO WEBPEdia
wELcOME to webpeDIA

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net