Home > Software engineering >  How can I make string upper and lower
How can I make string upper and lower

Time:09-03

I want to make function which make upper string to lower and lower string to upper.(one by one) Code was like below.

def Upper_lower(words):

  for i in range(len(words)):
    if words[i].isupper() == True:
      words[i] = words[i].lower()
     
    else:
      words[i] = words[i].upper()

  return words

Upper_lower('AAABBBcccddd')

The answer was 'aaabbCCCDDD' But I got error like this.

enter image description here

How can I fix this code?

CodePudding user response:

Manipulate your code like following if you don't want to use inbuilt function "swapcase"

def Upper_lower(words):
    res =""
    for i in words:
        if i.isupper() == True:
            res =res i.lower()
        else:
            res =res i.upper()

    return res

print(Upper_lower('AAABBBcccddd'))

else use this one

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

str1 ='AAABBBcccddd'
swap(str1)
  • Related