Home > Net >  Return word with string between each occurrence
Return word with string between each occurrence

Time:05-25

I am using python 3.10.4 and I need help. What would be the formula if you need to repeat a word with a string between each occurrences? For example we have the word dog and the string ', between ' how do I do it so that between is between each dog occurrences? This is an example of what I need: 'dog, between dog, between dog, between dog'

CodePudding user response:

You can achieve it with join like so:

res = ', between '.join(['dog'] * 4)
print(res)

CodePudding user response:

You can get that one by using the join() function

result = ', between '.join(arr)
print(result)

Where arr is the list containing the required word for 'N' number of times
Check the documentation for join() function here

CodePudding user response:

you can use join method as in this example:

>>> 'separator'.join(['foo', 'bar'])
fooseparatorbar

>>> '-'.join(['foo'] * 3)
foo-foo-foo
  • Related