1

I have a list of strings called args. The list looks like this: ['I', 'eat', 'eggs']

I want to generate a string that is all the items in the list, seperated by spaces. ("I eat eggs")

How can I do this if the list always has a different amount of items?

  • 1
    I believe this is answered by https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string – astrochun Feb 22 '21 at 14:09

2 Answers2

1

Use this code:

listToStr = ' '.join([str(elem) for elem in args]) 

or

listToStr = ' '.join(args) by @Jasmijn

Subbu VidyaSekar
  • 2,503
  • 3
  • 21
  • 39
0

You can use this:

args = ['I', 'eat', 'rice']
myString = ''
for i in args[:-1]:
  myString += i
  myString += " " 
myString += args[-1]  
print(myString)  
Faizun Faria
  • 151
  • 1
  • 6