sWAP cASE

You are given a string . 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 .
Constraints
Output Format
Print the modified string .
Sample Input
HackerRank.com presents "Pythonist 2".
Sample Output
hACKERrANK.COM PRESENTS "pYTHONIST 2".



Download Source Code

Post a Comment

8 Comments

  1. explain , return(' ').join(map(change,s))

    ReplyDelete
  2. def change(x):
    if str.islower(x):
    return str.upper(x)
    else:
    return str.lower(x)
    def swap_case(s):
    return ('').join(map(change,s))
    input give than explain

    ReplyDelete
  3. def swap_case(s):
    new=[ch.lower() if ch.isupper() else ch.upper() for ch in s]
    new=''.join(new)
    return new

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

    Easiest and efficient way is to use list comprehension

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Here is my solution for swap case. i assure you this is the easiest way and does not use map or join .Feel free to mail at aasthas114@gmail.com for for queries.

    def swap_case(s):
    new = ""
    for i in range(len(s)):
    if str.isupper(s[i]):
    new = new + str.lower(s[i])

    elif str.islower(s[i]):
    new = new + str.upper(s[i])

    else:
    new = new + str(s[i])

    return (new)


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

    ReplyDelete
  7. def swap_case(s):
    a=""
    for i in s:
    if i==i.lower():
    a=a+(i.upper())
    else:
    a=a+(i.lower())
    return (a)

    ReplyDelete