Reputation: 1
I was wondering if there was a way to repeat certain characters from a list. For instance:
characters = ['t' 'a' 'c' 'o']
print(characters)
That would print ['taco']
.
How would I get it to print ['tacooo']
?
Upvotes: 0
Views: 89
Reputation: 384
I think you can try this way to solve your problem.
characters = ['t', 'a', 'c', 'o']
result = ""
n=3
for i in range(len(characters)):
if(characters[i] != 'o'):
result = result+characters[i]
else:
result = result + (characters[i]*n)
print([result]);
Upvotes: 0
Reputation: 15478
If you want to repeat character n times in n index look at this example:
characters = ['t','a','c','o']
print(''.join(characters[0:3])+characters[3]*3)
Just multiply n with a single index or to this for multiple then join the items. Then concatenate with +.
Upvotes: 1