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".
8 Comments
explain , return(' ').join(map(change,s))
ReplyDeletedef change(x):
ReplyDeleteif 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
this is not the correct set of codes
Deletedef swap_case(s):
ReplyDeletenew=[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
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHere 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.
ReplyDeletedef 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)
def swap_case(s):
ReplyDeletea=""
for i in s:
if i==i.lower():
a=a+(i.upper())
else:
a=a+(i.lower())
return (a)