How do you join a list with a space in python?

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.

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.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list of strings, write a Python program to convert the given list of strings into a space-separated string.

    Examples:

    Input : ['geeks', 'for', 'geeks']
    Output : geeks for geeks
    
    Input : ['Python', 'Language']
    Output : Python Language
    

    Approach #1 : Python string translate[]

    The string translate[] method returns a string where each character is mapped to its corresponding character in the translation table. The limitation of this method is that it does not work for Python 3 and above versions.

    def convert[lst]:

        return str[lst].translate[None, '[],\'']

    lst = ['geeks', 'for', 'geeks']

    print[convert[lst]]

     
    Approach #2 : join[] function
    The join[] method is a string method and returns a string in which the elements of sequence have been joined by str separator. In this approach space is the separator.

    def convert[lst]:

        return ' '.join[lst]

    lst = ['geeks', 'for', 'geeks']

    print[convert[lst]]

    How do you join a list with spaces?

    If you need to join the list of strings with spaces, call the method on a string that contains a space. Copied! Similarly, you can use a newline [ \n ] to join the strings in the list with a newline character.

    How do you join a string with spaces in Python?

    Python concatenate strings with space We can also concatenate the strings by using the space ' ' in between the two strings, and the “+” operator is used to concatenate the string with space. To get the output, I have used print[my_str]. You can refer to the below screenshot for the output.

    How do you put a space in a list in Python?

    We add space in string in python by using rjust[], ljust[], center[] method. To add space between variables in python we can use print[] and list the variables separate them by using a comma or by using the format[] function.

    How do you make a list of space

    To convert a space-separated string to a list in Python, call the str.split[] method:.
    sentence = "This is a test".
    words_list = sentence. split[].
    print[words_list].

    Chủ Đề