python - 如何在字符串中交替更改大写和小写?
问题描述
我想从给定的字符串创建一个新的字符串,大写和小写交替。
我尝试遍历字符串并首先将大写更改为新字符串,然后再次将小写更改为另一个新字符串。
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)])
推荐阅读
- android - Android 在横向上的 requestPermissions 但在旋转到横向之前以纵向显示的对话框
- node.js - jwt.sign() 我得到错误:0909006C:PEM 例程:get_name:no start line 取决于选项
- docker - Docker 教程: docker run -it ubuntu ls / 使用 git bash (Windows) 给我一个无文件/目录错误
- validation - solana:找不到命令
- sql - 如何找到两个最近时间sql的时间差
- storage - 未知的 libmono-static-sgen.lib 文件
- javascript - 从外部监控 google sheet change 事件
- python-3.x - 这个 Python 3.8 函数有什么问题?
- python - 结果图像未在 VSCode 中显示
- html - 如何在primeng表中使用ng2搜索过滤器