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)
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)
here is the code for this
def swap_case(s): return s.swapcase()
return ''.join(c.lower() if c.isupper() else c.upper() if c.islower() else c for c in s)
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)
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)