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: –
Note: translate() method not work for Python 3 and above versions.
Ways Python Convert List to space-separated String
- string translate()
- join() function
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)
Output: EyeHunts Python Tutorial
This is the easiest method until you need the joined string for something else. Otherwise, use
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]
list2 = ['a', 'b', 'c', 'd', 'e']
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.
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:
Do comment if you have any doubts and input on this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.