python - 如何在字符串中交替更改大写和小写?

标签 python python-3.x

我想从给定的字符串创建一个新的字符串,交替使用大写和小写。

我尝试遍历字符串并首先将大写字母更改为新字符串,然后再次将小写字母更改为另一个新字符串。

def myfunc(x):
    even = x.upper()
    lst = list(even)
    for itemno in lst:
        if (itemno % 2) !=0:
            even1=lst[1::2].lowercase()
        itemno=itemno+1   
    even2=str(even1)
    print(even2)

因为我无法更改给定的字符串,所以我需要一种创建新字符串替代大写字母的好方法。

最佳答案

这是一个在线者

"".join([x.upper() if i%2 else x.lower() for i,x in enumerate(mystring)])

关于python - 如何在字符串中交替更改大写和小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54231976/

相关文章:

Python无法安装包

python - 如何从 Python 中的列表中获取具有相应出现次数的唯一值?

Python - 比较两列特征,返回两者不共同的值

python - 通过网络或任何其他 key 交换进行 Diffie-Hellman key 交换

python - 我的程序过滤字符串列表时出现问题

创建字典时遇到Python脚本,对于字典的每个键,有多个值(300个条目)列表

python - Python 中的简单 URL GET/POST 函数

python - 在 Web2py Python 中格式化日期

Python 不被解释而是显示编译行为

python - try-except 和 RuntimeWarning