字符串在Python中是不可变的,因此您需要创建一个新的字符串对象。一种方法:
indices = set([0, 7, 12, 25])s = "i like stackoverflow and python"print("".join(c.upper() if i in indices else c for i, c in enumerate(s)))印刷
I like StackOverflow and Python

字符串在Python中是不可变的,因此您需要创建一个新的字符串对象。一种方法:
indices = set([0, 7, 12, 25])s = "i like stackoverflow and python"print("".join(c.upper() if i in indices else c for i, c in enumerate(s)))印刷
I like StackOverflow and Python