Python Example sWAP cASE challenge in HackerRank

Problem

You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.

For Example:

Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2

Input Format

A single line containing a string S.

Constraints

0 < len(S) <= 1000

Output Format

Print the modified string S.

Sample Input 0

HackerRank.com presents "Pythonist 2".

Sample Output 0

hACKERrANK.COM PRESENTS "pYTHONIST 2".

Solution

def swap_case(s):

    temp = []
    l = list(s)

    for i in l:
        j = ""
        if i.islower():
            j = i.upper()
        elif i.isupper():
            j = i.lower()
        else:
            temp.append(i)
        temp.append(j)
    
    r = ''.join(temp)
    
    return r

s = input()
result = swap_case(s)
print(result)

Latest Posts

Feel free to share this post!

Scroll to Top