Skip to content

Python Convert List into a space-separated string & Print

  • by

You can use a translate() method to Convert the Python list into a space-separated list. Another method is using a join() function.

One more way is to apply the list as separate arguments: – print(*L)

Note: translate() method not work for Python 3 and above versions.

Ways Python Convert List to space-separated String

  • string translate()
  • join() function
  • print(*list_name)

Let’s see How to print a space-separated list in Python

1. string translate()

This method does not work for Python 3 and above versions. So we can do a test it’s an example.

2. join() function

The Joint() method returns a string in which the elements of the sequence have been joined by an str separator.

lst = ['EyeHunts', 'Python', 'Tutorial']
strlist = ' '.join(lst)
print(strlist)

Output: EyeHunts Python Tutorial

3. print(*list_name)

This is the easiest method until you need the joined string for something else. Otherwise, use str.join().

print() take care of converting each element to a string. You can control the separator by setting the sep keyword argument:

list1 = [1, 2, 3, 4, 5]
print(*list1)

list2 = ['a', 'b', 'c', 'd', 'e']
print(*list2)

Output:

1 2 3 4 5
a b c d e

Q: How to Python print list elements on one line?

Answer: Just use * before the list in the print method.

print(*list)

This will print the list separated by spaces.

Or if you want comma-separated then use this code:-

a = [1, 2, 3, 4, 5]
print(*a, sep = ',')

You get this output: 1,2,3,4,5

Do comment if you have any doubts and input on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading