sWAP cASE in Python - HackerRank Solution

sWAP cASE in Python - HackerRank Solution
sWAP cASE in Python - HackerRank Solution


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 :

HackerRank.com presents "Pythonist 2".

Sample Output :

hACKERrANK.COM PRESENTS "pYTHONIST 2".



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# sWAP cASE in Python - HackerRank Solution
def swap_case(s):

    # sWAP cASE in Python - HackerRank Solution START
    Output = '';
    for char in s:
        if(char.isupper()==True):
            Output += (char.lower());
        elif(char.islower()==True):
            Output += (char.upper());
        else:
            Output += char;
    return Output;
    # sWAP cASE in Python - HackerRank Solution END

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
3 Comments
  • Ankit Mitra
    Ankit Mitra Tuesday, January 19, 2021

    Thank You very much. :)

  • Anonymous
    Anonymous Wednesday, June 16, 2021

    Just use the string.swapcase() function, much easier.

  • Unknown
    Unknown Wednesday, January 26, 2022

    i have a small doubt, in this code in u use semicolon .in python code its not possible to use semicolon can u expalin this

Add Comment
comment url