0

Topic 7: Question 4 Write the function changeCase(word) that changes the case of all the letters in a word and returns the new word.

Examples

>>> changeCase('aPPle')
"AppLE"
>>> changeCase('BaNaNa')
'bAnAnA'

I am a beginner in python where is my error ?

def changeCase(word):
    return ''.join(c.upper() if c in 'aeiou' else c.lower() for c in word)
3kstc
  • 1,871
  • 3
  • 29
  • 53
Ramy George
  • 49
  • 1
  • 1
  • 4

6 Answers6

18

Use str.swapcase:

>>> 'aPPle'.swapcase()
'AppLE'
>>> 'BaNaNa'.swapcase()
'bAnAnA'
AMC
  • 2,642
  • 7
  • 13
  • 35
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

My solution to the problem, no need to "manually" change a case of a letter as above.

def changeCase(word):
    newword = ""                     # Create a blank string
    for i in range(0, len(word)):
        character = word[i]          # For each letter in a word, make as individual viarable
        if character.islower()== False: # Check if a letter in a string is already in upper case
            character = character.lower()  # Make a letter lower case
            newword += character           # Add a modified letter in a new string
        else:
            character = character.upper()  # Make a letter upper case
            newword += character           # Add a modified letter in a new string

    return newword                         # Return a new string
Pifko6
  • 17
  • 4
  • 1
    _as above_ Above where? Answers can and do move around relative to each other. Also, that for loop should be simplified to `for character in word:`, and the `== False` is both unidiomatic and superfluous. – AMC May 08 '20 at 15:50
-1
def swap_case(s):
    input_list = list(s)
    return "".join(i.lower() if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" else i.upper() for i in input_list)

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)
Nikhil
  • 19
  • 1
  • 8
  • 4
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. For example, I don't see how `ä` becomes`Ä` and vice versa. – Toby Speight Oct 06 '17 at 12:59
  • A more reasonable thing to do would be to use [`string.ascii_uppercase`](https://docs.python.org/3/library/string.html#string.ascii_uppercase) then explicitly writing all letters... – Tomerikoo Jun 04 '20 at 10:53
-1
#Here is another example to convert lower case to upper case and vice versa   
while not bothering other characters:
#Defining function
def swap_case(s):
    ls=[]
    str=''
    # converting each character from lower case to upper case and vice versa and appending it into list
    for i in range(len(s)):
        if ord(s[i]) >= 97:
            ls.append(chr((ord(s[i]))-32)) #ord() and chr() are inbuild methods
        elif ord(s[i]) >= 65 and ord(s[i]) <97:
             ls.append(chr((ord(s[i]))+32))
        # keeping other characters
        else:
            ls.append(chr(ord(s[i])))
    # converting list into string
    for i in ls:
        str+=i
    return(str)
if __name__ == '__main__':
    s = input()
    # calling function swap_case()
    result = swap_case(s)
    print(result)


input: www.SWAPCASE@program.2
Output: WWW.swapcase@PROGRAM.2
  • 1
    There are many issues with this code: Naming a variable `str` is a bad idea. Why use `for i in range(len(s)):` instead of `for elem in s:` ? In the same loop, you write `append` three different times, just use a variable to save the new character, and append it at the end. To convert a list to a string, just use `"".join(some_list)`. There shouldn't be any parentheses in `return()`. – AMC May 08 '20 at 15:48
-2
let="alpHabET"

print(let.swapcase())

#o/p == ALPhABet
dbc
  • 104,963
  • 20
  • 228
  • 340
  • 3
    How does this differ from [this answer](https://stackoverflow.com/a/27231431/3744182) by [falsetru](https://stackoverflow.com/users/2225682/falsetru) from Dec 01, 2014 which also says to use `swapcase()`? – dbc Mar 01 '20 at 16:50
-2
txt  = "aPpLe"
print (txt.swapcase())

The SwapCase function will return a copy of the string with uppercase characters converted to lowercase and vice versa.

for more : see documentation here

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Mitesh
  • 1,544
  • 3
  • 13
  • 26
  • 1
    This is the same solution as [the top answer](https://stackoverflow.com/a/27231431/11301900), which is 6 years older. – AMC May 08 '20 at 15:52