Python Convert List to String with Space Example

By Hardik Savani October 30, 2023 Category : Python

Hey Friends,

I am going to show you an example of python convert list into string with spaces. I would like to show you python convert list to string space separated. I would like to share with you python convert list to space separated string with quotes. Here you will learn how to convert list to string with whitespace in python. you will do the following things for python split list of strings by space.

There are several ways to convert the list into a string with space separated in python. we will use join() and for loop to convert list to string spaces separated. so let's see the below examples.

You can use these examples with python3 (Python 3) version.

let's see below a simple example with output:

Example 1:

main.py

myList = ['one', 'two', 'three', 'four', 'five']
  
# Convert List into String
newString = ' '.join(myList)
  
print(newString)

Output:

one two three four five

Example 2:

main.py

myList = [1, 2, 3, 4, 5]
  
# Convert List into String
newString = ' '.join(str(x) for x in myList)
  
print(newString)

Output:

1 2 3 4 5

Example 3:

main.py

myList = ['one', 'two', 'three', 'four', 'five']
  
# Convert List into String
newString = '';
  
for str in myList:
    newString += str + ' ';
  
print(newString)

Output:

one two three four five

I hope it can help you...

Tags :
Shares