0

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']?

Rafael de Bem
  • 641
  • 7
  • 15
yessir
  • 1

2 Answers2

1

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 +.

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

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]);

Yeasin Arafath
  • 365
  • 4
  • 11