sWAP cASE

Sort by

recency

|

2032 Discussions

|

  • + 0 comments

    here is the code for this

    def swap_case(s): return s.swapcase()

  • + 0 comments

    return ''.join(c.lower() if c.isupper() else c.upper() if c.islower() else c for c in s)

  • + 0 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

    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)

  • + 1 comment

    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)

    1. Code
    • + 0 comments

      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)

  • + 0 comments
    def swap_case(s):
        my_list = []
        for i in s:
            if 65 <= ord(i) <= 90:
                i = chr(ord(i) + 32)
                my_list.append(i)
            elif 97 <= ord(i) <= 122:
                i = chr(ord(i) - 32)
                my_list.append(i)
            else:
                my_list.append(i)
        return ''.join(my_list)
    
    if __name__ == '__main__':
        s = input()
        result = swap_case(s)
        print(result)