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.
I'll be checking out some of these other scripts for performance. My concept - I thought it'd be fun to build off of the list index in my loop. It works. ChatGPT claimed that this list-driven syntax will have better performance than string concatenation, e.g. the += letter.lower() syntax.
def swap_case(s):
newstring = list(s) # Create an empty string to store the new text
for index in range(len(newstring)):
if newstring[index].isupper():
newstring[index]= newstring[index].lower()
else:
newstring[index]= newstring[index].upper()
return ''.join(newstring) #Combining the separate characters back into a string
if name == 'main':
s = input()
result = swap_case(s)
print(result)
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 →
I'll be checking out some of these other scripts for performance. My concept - I thought it'd be fun to build off of the list index in my loop. It works. ChatGPT claimed that this list-driven syntax will have better performance than string concatenation, e.g. the += letter.lower() syntax.
def swap_case(s): newstring = list(s) # Create an empty string to store the new text
if name == 'main': s = input() result = swap_case(s) print(result)