How to concatenate string with list in python

I wanted to build a string from a list.

I used the string.join() command, but if I have :

['hello', 'good', 'morning']

I get : hellogoodmorning

Is there a method that allows me to put a space between every word ? (without the need to write a for loop)

kind regards.

Adam Wagner

14.7k7 gold badges52 silver badges65 bronze badges

asked Dec 17, 2011 at 16:42

How to concatenate string with list in python

Lucas KauffmanLucas Kauffman

6,54815 gold badges59 silver badges83 bronze badges

2

All you need to do is add the space in front of join.

 ' '.join(list)

answered Dec 17, 2011 at 16:45

Lance CollinsLance Collins

3,2558 gold badges40 silver badges55 bronze badges

>>> ' '.join(['hello', 'good', 'morning'])
'hello good morning'

The standard and best way to join a list of strings. I cannot think of anything better than this one.

How to concatenate string with list in python

Rodrigue

3,5652 gold badges37 silver badges49 bronze badges

answered Dec 17, 2011 at 16:44

This does what you want:

" ".join(['hello', 'good', 'morning'])

Generally, when you are calling join() on some string you use " " to specify the separator between the list elements.

How to concatenate string with list in python

Alexander

98.9k27 gold badges186 silver badges185 bronze badges

answered Dec 17, 2011 at 16:45

bofh.atbofh.at

5603 silver badges6 bronze badges

' '.join(...) is the easiest way as others have mentioned. And in fact it is the preferred way to do this in all cases (even if you are joining with no padding, just use ''.join(...)).

While it still has some useful functions... most of the string modules functions have been made methods on the str type/object.

You can find the full list of deprecated string-functions (including join) in the Deprecated string functions section of the python docs.

answered Dec 17, 2011 at 19:54

Adam WagnerAdam Wagner

14.7k7 gold badges52 silver badges65 bronze badges

>>> " ".join(['hello', "good", "morning"])
'hello good morning'

or you can use the string.join() function, which uses a single space as the default separator.

>>> help(string.join)
Help on function join in module string:

join(words, sep=' ')
    join(list [,sep]) -> string

    Return a string composed of the words in list, with
    intervening occurrences of sep.  The default separator is a
    single space.

example:

>>> import string
>>> string.join(['hello', "good", "morning"])
'hello good morning'

answered Dec 17, 2011 at 16:45

Fredrik PihlFredrik Pihl

43.3k7 gold badges81 silver badges128 bronze badges

Not the answer you're looking for? Browse other questions tagged python list or ask your own question.

How do you concatenate a list in python?

1. Concatenation operator (+) for List Concatenation. The '+' operator can be used to concatenate two lists. It appends one list at the end of the other list and results in a new list as output.

How do you concatenate all elements in a list?

To concatenate a list of integers an empty list is created as newlist = []. The extend method() adds all the elements of the list till the end. To concatenate the list on integers “+” is used. The print(newlist) is used to get the output.

How do I concatenate strings in python?

Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.