20

I am learning Python and am working on this exercise:

Create a function that will return another string similar to the input string, but with its case inverted. For example, input of "Mr. Ed" will result in "mR. eD" as the output string.

My code is:

name = 'Mr.Ed'
name_list = []

for i in name:
    if i.isupper():
        name_list.append(i.lower())
    elif i.islower():
        name_list.append(i.upper())
    else:
        name_list.append(i)

    print(''.join(name_list))

Is there a simpler or more direct way to solve it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
liyuhao
  • 365
  • 1
  • 2
  • 6

9 Answers9

117

You can do that with name.swapcase(). Look up the string methods (or see the older docs for legacy Python 2).

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
wenzul
  • 3,948
  • 2
  • 21
  • 33
26

Your solution is perfectly fine. You don't need three branches though, because str.upper() will return str when upper is not applicable anyway.

With generator expressions, this can be shortened to:

>>> name = 'Mr.Ed'
>>> ''.join(c.lower() if c.isupper() else c.upper() for c in name)
'mR.eD'
ch3ka
  • 11,792
  • 4
  • 31
  • 28
16

Simply use the swapcase() method:

name = "Mr.Ed"
print(name.swapcase())

Output: mR.eD

Explanation:
The method swapcase() returns a copy of the string in which all the case-based characters have had their case swapped.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jorvis
  • 386
  • 5
  • 13
4

In python, an inbuilt function swapcase() is present which automatically converts the case of each and every letter. Even after entering the mixture of lowercase and uppercase letters it will handle it properly and return the answer as expected.

Here is my code:

str1 = input("enter str= ")
res = str1.swapcase()
print(res)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Shehreen Khan
  • 75
  • 1
  • 9
1
name='Mr.Ed'
print(name.swapcase())
0

https://github.com/suryashekhawat/pythonExamples/blob/master/string_toggle.py

    def toggle(mystr):
        arr = []
        for char in mystr:
            if char.upper() != char:
                char=char.upper()
                arr.append(char)
            else:
                char=char.lower()
                arr.append(char)
        return ''.join(map(str,arr))
    user_input = raw_input()
    output = toggle(user_input)
    print output
0

The following program is for toggle case

name = input()
for i in name:
    if i.isupper():
        print(i.lower(), end='')
    else:
        print(i.upper(), end='')
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 5
    The quality of this answer is really quite low. Obviously the formatting could be improved. Furthermore, for an answer to be useful, one needs to comment and explain. Finally I don't really see a difference with other answers. – Tom de Geus Jun 27 '18 at 15:28
0

Note: "Moved" the answer from [SO]: Python: Replace lower case to upper, and vice versa, simultaneously via ReGex lib (so more people can benefit out of it).

This is more like an exercise: I wanted to do a comparison between different variants (and apparently ReggaExp tends to be slower than string manipulations - and (as expected) swapcase is waaay faster than anything else).

code00.py:

#!/usr/bin/env python

import re
import string
import sys
import timeit


def swap_case_re(s):
    return re.sub(r"([A-Z]+)|([a-z]+)", lambda arg: arg.group(1).lower() if arg.group(1) else arg.group(2).upper(), s)


def swap_case_s0(s):
    return "".join(c.lower() if "A" <= c <= "Z" else c.upper() if "a" <= c <= "z" else c for c in s)


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


def swap_case_s2(s):
    return "".join(chr(c + 0x20 if 0x41 <= c <= 0x5A else c - 0x20 if 0x61 <= c <= 0x7A else c) for c in s.encode())


def swap_case_s3(s):
    return "".join(c.lower() if c.isupper() else c.upper() for c in s)


def swap_case_s4(s, tbl="".maketrans(string.ascii_uppercase + string.ascii_lowercase, string.ascii_lowercase + string.ascii_uppercase)):
    return s.translate(tbl)


FUCS = (
    swap_case_re,
    swap_case_s0,
    swap_case_s1,
    swap_case_s2,
    swap_case_s3,
    swap_case_s4,
)


def test_acc():
    print("Test accuracy:")
    for s in ("Www.GooGle.com",):
        for func in FUCS:
            print("{:s}({:s}): {:s}".format(func.__name__, s, func(s)))


def test_perf(count=1000000):
    print("\nTest performance:")
    for func in FUCS:
        print("{:s}: {:.3f}".format(func.__name__, timeit.timeit(lambda: func("Www.GooGle.com"), number=count)))


def main(*argv):
    test_acc()
    test_perf()


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

Output (Win):

[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q076068146]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ./code00.py
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] 064bit on win32

Test accuracy:
swap_case_re(Www.GooGle.com): wWW.gOOgLE.COM
swap_case_s0(Www.GooGle.com): wWW.gOOgLE.COM
swap_case_s1(Www.GooGle.com): wWW.gOOgLE.COM
swap_case_s2(Www.GooGle.com): wWW.gOOgLE.COM
swap_case_s3(Www.GooGle.com): wWW.gOOgLE.COM
swap_case_s4(Www.GooGle.com): wWW.gOOgLE.COM

Test performance:
swap_case_re: 5.039
swap_case_s0: 3.607
swap_case_s1: 0.315
swap_case_s2: 3.034
swap_case_s3: 2.700
swap_case_s4: 0.535

Done.
CristiFati
  • 38,250
  • 9
  • 50
  • 87
-1

changeCase lambda method reverses the case of string if the character is alpha then perform (chr(ord(char)^32) which flips the case of a alphabet. ''.join([]) converts the list to a string.

  • ord returns an Unicode code representing a character.
  • chr converts an integer to a character.

main.py

#!/usr/bin/env python3.10
changeCase = lambda x: ''.join([(chr(ord(v)^32) if 65 <= ord(v) <= 90 or 97 <= ord(v) <= 122 else v) for v in x])

print(changeCase("Dev Parzival"))
print(changeCase("Hello World!"))
print(changeCase("Haÿ"))

Output:

$ chmod +x main.py
$ ./main.py
dEV pARZIVAL
hELLO wORLD!
hAÿ
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • Doesn't work for all non-ASCII characters, for example: `"Haÿ"` -> `"hAß"`. (Should be `"hAŸ"`) – wjandrea Mar 22 '23 at 15:28
  • @wjandrea thanks for pointing out the bug because of non-ASCII characters. I might haven't realized it. Now it supports non-ASCII characters as well. – Udesh Apr 08 '23 at 09:43
  • 2
    Now it doesn't do non-ASCII characters at all... Like I said, the output should be `"hAŸ"`, with a capital `Ÿ`. Anyway, I don't see any reason to try to roll your own solution when you could just use `str.swapcase()`, like others have answered. – wjandrea Apr 08 '23 at 15:42