Seeing the answers above - except the mostly voted one (which doesn't fully cover the request) - it was mainly division in a loop. Why not keep it more "programmatic", even if yet not that pythonic?
Here is example based on original question (no division used):
sample = "This is a string"
n = 3 # I want to iterate over every third element
i = 1
for x in sample:
if n == i:
i = 0
# do something with x
else:
# do something else with x
i += 1
If you sill like to process with valid index:
sample = "This is a string"
n = 3 # I want to iterate over every third element
# (!) here do something else with all elements within list/substring: sample[0:n-1]
for i in range(n-1, len(sample), n):
# (!) here do something with element: sample[i]
# (!) here do something else with all elements within list/substring: sample[i+1:i+n]
And now coming back to solution with division, but as a single line:
sample = "This is a string"
n = 3 # I want to iterate over every third element
# you can call below your own correspondent functions (according to case) instead of print() ;-)
_ = [print('Do something: ' + sample[i]) if 0 == ((1+i) % n) else print('Do something else: ' + sample[i]) for i in range(len(sample))]
for i,x in enumerate(sample,1):
– Palermo