-6

I have a list and a string below:

fruits = ['banana', 'apple', 'plum']
mystr = 'i like the following fruits: '

How can I concatenate them so I get (keeping in mind that the enum may change size) :

i like the following fruits: 

banana
apple
plum

I need the mystr in the starting and fruit names one by one as shown

2
  • 1
    Tru mystr+' '.join(fruits) Commented Mar 21, 2018 at 13:31
  • 1
    To achieve the exact desired result as in your question, you can do this: print("\n\n".join([mystr, ",".join(fruits)])). Commented Mar 21, 2018 at 13:34

1 Answer 1

2

You can use str.format, and str.join to get your required output.

Ex:

fruits = ['banana', 'apple', 'plum']
mystr = 'i like the following fruits: \n\n{}'.format("\n".join(fruits))
print(mystr)

Output:

i like the following fruits: banana, apple, plum
Sign up to request clarification or add additional context in comments.

3 Comments

can i get the output as i mentioned in my question. (edited)
Sure. Updated solution.
Can i have the code ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.