Python ProgrammingPython Programming

How to capitalize every 3rd letter of string in Python?

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