I have a string where a character ('@') needs to be replaced by characters from a list of one or more characters "in order" and "periodically". So for example I have
'ab@cde@@fghi@jk@lmno@@@p@qrs@tuvwxy@z'
and want
'ab1cde23fghi1jk2lmno312p3qrs1tuvwxy2z'
for replace_chars = ['1', '2', '3']
The problem is that in this example there are more @ in the string than I have replacers.
This is my try:
result = ''
replace_chars = ['1', '2', '3']
string = 'ab@cde@@fghi@jk@lmno@@@p@qrs@tuvwxy@z'
i = 0
for char in string:
if char == '@':
result += replace_chars[i]
i += 1
else:
result += char
print(result)
but this only works of course if there are not more than three @ in the original string and otherwise I get IndexError.
Edit: Thanks for the answers!