1

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?

stephsmith
  • 171
  • 5
  • Please explain how your question is related to following tags you have used: `pandas`, `dataframe`? – Daweo Oct 27 '21 at 11:26
  • we have string texts in each row of a dataframe. But the methods didn't seem to work on rows of a dataframe – stephsmith Oct 27 '21 at 11:31
  • 1
    Does this answer your question? [How to concatenate items in a list to a single string?](https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string) – Mykola Zotko Oct 27 '21 at 11:32

3 Answers3

3
", ".join(["Welcome to India"]*3)
Riley
  • 2,153
  • 1
  • 6
  • 16
1

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
FourBars
  • 475
  • 2
  • 14
0

Define Function and you can use it.

def commaFunc(value, count):
    buffer = []
    for x in range(count):
        buffer[x] = value
    return ",".join(buffer)
data_m
  • 124
  • 1
  • 9