Python 反转字符串大小写的更好方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26385823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:25:31  来源:igfitidea点击:

better way to invert case of string

python

提问by liyuhao

I am learning python and meet an exercise:

我正在学习python并遇到一个练习:

Strings. 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.

字符串。创建一个函数,该函数将返回另一个类似于输入字符串的字符串,但大小写颠倒。例如,输入“Mr. Ed”将导致“mR. eD”作为输出字符串。

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 any better way to solve it? My solution seems strange.

有没有更好的方法来解决它?我的解决方案似乎很奇怪。

采纳答案by ch3ka

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

您的解决方案非常好。不过,您不需要三个分支,因为str.upper()无论如何都会在 upper 不适用时返回 str 。

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'

回答by wenzul

You can do that with name.swapcase(). Lookup the string methods.

你可以用name.swapcase(). 查找字符串方法

回答by Surya Pratap

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

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

回答by Jorvis

Simply use the swapcase() method :

只需使用 swapcase() 方法:

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

Output : mR.eD

输出:mR.eD

-> This is just a two line code.

-> 这只是两行代码。

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

说明:
swapcase() 方法返回字符串的副本,其中所有基于大小写的字符都已交换大小写。

Happy Coding!

快乐编码!

回答by krishna_mannava

#the following program is for toggle case
name=input()
for i in name:
    if i.isupper():
        print( i.lower(),sep='',end='')
    else:
        print( i.upper(),sep='',end='')

回答by Prabal Tiwari

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

回答by Shehreen Khan

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.

在 python 中,存在一个内置函数 swapcase() 自动转换每个字母的大小写。即使在输入小写和大写字母的混合后,它也会正确处理并按预期返回答案。

Here is my code:

这是我的代码:

    str1=input("enter str= ")
    res=str1.swapcase()
    print(res)