What you're asking for already exists: str.join. join is only doing the hard part; we still need something for the easy part… but that's the easy part.
Also, as others have pointed out, you can use the * operator on strings to get you most of the way there. But, despite having nearly written that yourself, I get the feeling you don't understand why that's most of the way there.
You can even combine the two: delim.join([string] * n). (Here, I'm repeating a list rather than the string itself, because otherwise I'd just get one big string "spamspamspamspamspam" instead of five separate "spam"s to join up.)
But, I'm going to assume this is a homework assignment or a self-teaching attempt, and you want to know how to write it yourself from scratch.
Normally, when you want to repeat something, you want a loop.
If you want to repeat something for each element in the list spam, you write the loop for element in spam:.
But what if you want to repeat it 5 times? There's no "each element in the number 5". You need for find something that has 5 elements. And range(5) is perfect for that. So:
def repeat(string, n, delim):
for i in range(n):
# something
Next, you already know how to concatenate strings, with the + operator. So, let's start with an empty string, and concatenate onto the end of it:
def repeat(string, n, delim):
result = ""
for i in range(n):
result = result + string + delim
return result
But this has a problem:
>>> repeat("spam", 5, ", ")
'spam, spam, spam, spam, spam, '
There's just the right amount of spam, but too many commas.
If you go back to the * solution, you'll see that this is the same thing as (string + delim) * n, we just got there the long way. Either way, you get the same problem.
How do you avoid that?
Here you have to get a bit tricky. You can treat the last value special, or the first one. Or you can look at result so far to see if you're at the start. Or you can just let it add the extra delimiter and then lop it off at the end. I'll show you one of those options, but you should try to write another one yourself. (Looking at result is probably the easiest, and the cleanest, once you figure out how to adjust the algorithm to make it possible.)
def repeat(string, n, delim):
result = ""
for i in range(n):
result = result + string + delim
return result[:-len(delim)]
The way I lopped off the extra delimiter at the end was by using slicing, with a negative index. You probably haven't learned that yet, which is why you should write a solution for yourself. (Plus, this is probably the hackiest of the solutions.)
Going all the way back to the top, if you play around with join, you'll see that it solves this problem for you. It's obviously not using magic, so you might think about looking at how it's implemented. Unfortunately, it's probably a big mess of C code somewhere in this file, not nice clear Python, so you probably wouldn't learn much.
nametwo = name * nis intended to be part of the functionrepeat(), this almost does what you want - the only thing left to do is to remove the lastdelimfrom the string before returning it - look up "string slicing" to do that.