125

How can I convert a list into a space-separated string in Python?

For example, I want to convert this list:

my_list = ["how", "are", "you"]

into the string "how are you".

The spaces are important. I don't want to get "howareyou".

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user1653402
  • 1,363
  • 3
  • 9
  • 7

6 Answers6

272
" ".join(my_list)

You need to join with a space, not an empty string.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
27

I'll throw this in as an alternative just for the heck of it, even though it's pretty much useless when compared to " ".join(my_list) for strings. For non-strings (such as an array of ints) this may be better:

" ".join(str(item) for item in my_list)
Community
  • 1
  • 1
Polaris
  • 293
  • 3
  • 6
12

For Non String list we can do like this as well

" ".join(map(str, my_list))
Athar
  • 963
  • 10
  • 23
5

So in order to achieve a desired output, we should first know how the function works.

The syntax for join() method as described in the python documentation is as follows:

string_name.join(iterable)

Things to be noted:

  • It returns a string concatenated with the elements of iterable. The separator between the elements being the string_name.
  • Any non-string value in the iterable will raise a TypeError

Now, to add white spaces, we just need to replace the string_name with a " " or a ' ' both of them will work and place the iterable that we want to concatenate.

So, our function will look something like this:

' '.join(my_list)

But, what if we want to add a particular number of white spaces in between our elements in the iterable ?

We need to add this:

str(number*" ").join(iterable)

here, the number will be a user input.

So, for example if number=4.

Then, the output of str(4*" ").join(my_list) will be how are you, so in between every word there are 4 white spaces.

Community
  • 1
  • 1
Abhay Tiwari
  • 51
  • 1
  • 3
1

you can iterate through it to do it

my_list = ['how', 'are', 'you']
my_string = " "
for a in my_list:
    my_string = my_string + ' ' + a
print(my_string)

output is

 how are you

you can strip it to get

how are you

like this

my_list = ['how', 'are', 'you']
my_string = " "
for a in my_list:
    my_string = my_string + ' ' + a
print(my_string.strip())

saif-py
  • 11
  • 2
-12

Why don't you add a space in the items of the list itself, like :
list = ["how ", "are ", "you "]

Anonymous
  • 1
  • 1
  • You have written this *answer* in a way that makes it appear to be more appropriate in the form of a comment. It would be more beneficial if you expanded this answer to explain ***why*** what you are suggesting works. – Andon M. Coleman Jan 12 '14 at 20:01
  • 1
    This is non-functional in a case where you do not have access to the list (e.g when generated with `input(a).split(b)`) – TheSola10 Sep 22 '16 at 07:28
  • we should not change the internal strings because it cause us loss of space – Deepak Yadav Jun 24 '18 at 15:02