Let's say we have a text "Welcome to India". And I want to multiply this string with 3, but to appear with comma delimitation, like "Welcome to India, Welcome to India, Welcome to India". The thing is I know this piece of code could work:
a = 'Welcome to India'
required = a * 3 // but this code is not comma-delimited.
Also, this piece of code doesn't work as well
required = (a ", ") * 3 // because it puts comma even at the end of the string
How to solve this problem?
CodePudding user response:
", ".join(["Welcome to India"]*3)
CodePudding user response:
Based on the part of your code that you say doesn't work well because it adds a comma to the end, you can modify it like this:
required = (a ", ") * 2 a
Or if you don't like it, you can use sum instead of multiply, it's not the optimal way, for sure, but it will work:
a = 'Welcome to India'
required = a ", " a ", " a
CodePudding user response:
Define Function and you can use it.
def commaFunc(value, count):
buffer = []
for x in range(count):
buffer[x] = value
return ",".join(buffer)