1

I'm trying to write a function which given a string returns another string that flips its lowercase characters to uppercase and viceversa.

My current aproach is as follows:

def swap_case(s):
    word = []
    for char in s:
        word.append(char)

    for char in word:
        if char.islower():
            char.upper()
        else:
            char.lower()

    str1 = ''.join(word)

However the string doesn't actually change, how should I alter the characters inside the loop to do so?

PS: I know s.swapcase() would easily solve this, but I want to alter the characters inside the string during a for loop.

George Francis
  • 462
  • 2
  • 7
  • 16
  • 1
    strings are immutable in Python. You are attempting to make the change in place, which will not work. You are going to have to store your result in a new variable. – idjaw Feb 05 '18 at 04:26
  • Yes, I know that, which is why created the list "word" and stored inside it every char from the original string, I'm trying to alter the list, not the string. – George Francis Feb 05 '18 at 04:28
  • 2
    You are doing `char.upper()` and `char.lower()`. You are not storing the result anywhere. Hence, why it looks like you are trying to do it *in place*. – idjaw Feb 05 '18 at 04:28
  • 1
    `char.upper()` _returns_ the uppercased character, but does not alter the original value. – John Gordon Feb 05 '18 at 04:28
  • The perhaps you should edit your question because that is not what you are implying: "but I want to alter the characters inside the string during a for loop." But again, the answer is still that strings are immutable, and you never change anything inside the list. BTW, to put the individual characters of a string in a list, simply use `word = list(s)` – juanpa.arrivillaga Feb 05 '18 at 04:30
  • Ahh, I see now, I'd forgotten that the char primitive doesn't exisit in Python, which is why nothing is getting changed, thank you. – George Francis Feb 05 '18 at 04:34
  • @GeorgeFrancis not sure how that is relevant. In Java, for example, there is a char type, and both them and String objects are also immutable and behave similarly to what you are seeing in Python. – juanpa.arrivillaga Feb 05 '18 at 04:53
  • Does this answer your question? [Swapping uppercase and lowercase in a string](https://stackoverflow.com/questions/36247173/swapping-uppercase-and-lowercase-in-a-string) – Georgy Aug 17 '20 at 00:28
  • If `swapcase` isn't an adequate solution, then there isn't a clear question here. It is not possible to "alter the characters inside the string" by any method from within Python, for any kind of alteration. – Karl Knechtel Aug 29 '22 at 14:10

6 Answers6

6
def swap_case(s):
    swapped = []

    for char in s:
        if char.islower():
            swapped.append(char.upper())
        elif char.isupper():
            swapped.append(char.lower())
        else:
            swapped.append(char)

    return ''.join(swapped)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
4

you can use swapcase.

string.swapcase(s)

Return a copy of s, but with lower case letters converted to upper case and vice versa.

Source : Python docs

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
2
>>> name = 'Mr.Ed' or name= ["M","r",".","E","d"]
>>> ''.join(c.lower() if c.isupper() else c.upper() for c in name)
'mR.eD'
Pyd
  • 6,017
  • 18
  • 52
  • 109
1

Your code is not working because you are not storing the chars after transforming them. You created a list with all chars and then, you access the values without actually saving them. Also, you should make your function return str1 or you will never get a result. Here's a workaround:

def swap_case(s):
    word = []
    for char in s:
        if char.islower():
            word.append(char.upper())
        else:
            word.append(char.lower())

    str1 = ''.join(word)
    return str1

By the way, I know you want to do it with a for loop, but you should know there are better ways to perform this which would be more pythonic.

JPYamamoto
  • 496
  • 6
  • 17
0

You can use this code:

s = "yourString"    
res = s.swapcase()    
print(res)

Which will output:

YOURsTRING
Robson
  • 2,008
  • 2
  • 7
  • 26
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 12 '21 at 01:26
0
def swap_case(s):
    x = ''
    for i in s:
        if i.isupper():
            i = i.lower()
        else:
            i = i.upper()
        x += ''.join(i)
    return x