我想从给定的字符串创建一个新的字符串,交替使用大写和小写。
我尝试遍历字符串并首先将大写字母更改为新字符串,然后再次将小写字母更改为另一个新字符串。
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/