How to Convert a List into a String with Spaces in Python

How do I convert a list into a string with spaces in Python?

" ".join(my_list)

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

Convert list of strings to space-separated string

Does this work for you

>>> my_dashes = ['_', '_', '_', '_']
>>> print ''.join(my_dashes)
____
>>> print ' '.join(my_dashes)
_ _ _ _

How to convert a string with spaces to list in python

# remove spaces and then put into list
[string.replace(" ", "")]

To add, if you don't add the spaces at the beginning, you don't need to use .replace(), just the brackets []

string = [fName + lName + location]

How to convert a list of strings with space delimited floats to a dataframe

You can do it with:

  • split function to separate string
  • float function to convert str to float
  • list comprehension

A one line instruction can do what you want

data = ['1.3 2.4 3.6','4.6 5 6.8','6.5 7.2 8.1']
dataframe = [[float(x) for x in e.split()] for e in data]

How to turn a list/tuple into a space separated string in python using a single line?

Use string join() method.

List:

>>> l = ["a", "b", "c"]
>>> " ".join(l)
'a b c'
>>>

Tuple:

>>> t = ("a", "b", "c")
>>> " ".join(t)
'a b c'
>>>

Non-string objects:

>>> l = [1,2,3]
>>> " ".join([str(i) for i in l])
'1 2 3'
>>> " ".join(map(str, l))
'1 2 3'
>>>

Convert a list of characters into a string

Use the join method of the empty string to join all of the strings together with the empty string in between, like so:

>>> a = ['a', 'b', 'c', 'd']
>>> ''.join(a)
'abcd'


Related Topics



Leave a reply



Submit