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?

3
  • Please explain how your question is related to following tags you have used: pandas, dataframe? Commented Oct 27, 2021 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 Commented Oct 27, 2021 at 11:31
  • 1
    Does this answer your question? How to concatenate items in a list to a single string? Commented Oct 27, 2021 at 11:32

3 Answers 3

3
", ".join(["Welcome to India"]*3)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, and in your case, required = ", ".join([a]*3).
But what do we do in case of having a dataframe? There it won't work
Then you're not asking your question well. You're asking for strings, not dataframes.
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

Comments

0

Define Function and you can use it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.