小册名称:Python合辑4-130个字符串操作示例
如何在 Python 中将字符串的第三个字母大写
s = "xxxyyyzzz"# convert to lista = list(s)# change every third letter in place with a list comprehensiona[2::3] = [x.upper() for x in a[2::3]]# back to a strings = ''.join(a)print(s)
s = "xxxyyyzzz"
# convert to list
a = list(s)
# change every third letter in place with a list comprehension
a[2::3] = [x.upper() for x in a[2::3]]
# back to a string
s = ''.join(a)
print(s)
Output:
xxXyyYzzZ