We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Problem itself is pretty straight forward you are given a string you gotta
return the upper case letter if it has a lower case letter and lower case letter to upper case
1. Make an empty string
2. check if the current i'th letter is even a letter or it's some number it's not letter and leave it and append it as it's into the list , if it's a letter then simply check the letter is upper or lower if it's lower then make it upper if it's upper then make it lower
3. Return the list as a string format because the problem seeks a string
def swap_case(s):
s1 = []
for item in s:
if item.isalpha():
if item.isupper():
s1.append(item.lower())
else:
s1.append(item.upper())
else:
s1.append(item)
return ''.join(s1)
s1 = []
for item in s:
if item.isalpha():
if item.isupper():
s1.append(item.lower())
else:
s1.append(item.upper())
else:
s1.append(item)
return ''.join(s1)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
sWAP cASE
You are viewing a single comment's thread. Return to all comments →
Problem itself is pretty straight forward you are given a string you gotta return the upper case letter if it has a lower case letter and lower case letter to upper case 1. Make an empty string 2. check if the current i'th letter is even a letter or it's some number it's not letter and leave it and append it as it's into the list , if it's a letter then simply check the letter is upper or lower if it's lower then make it upper if it's upper then make it lower 3. Return the list as a string format because the problem seeks a string def swap_case(s): s1 = [] for item in s: if item.isalpha(): if item.isupper(): s1.append(item.lower()) else: s1.append(item.upper()) else: s1.append(item) return ''.join(s1)
s1 = [] for item in s: if item.isalpha(): if item.isupper(): s1.append(item.lower()) else: s1.append(item.upper()) else: s1.append(item) return ''.join(s1)