28-Apr-2023
.
Admin
Hi Dev,
Now, let's see the post on how to convert a list to a string with space using Python. Here you will learn Python to convert the list into strings with spaces. step by step explains Python convert list to string space-separated. if you want to see an example of Python converting a list to a space-separated string with quotes then you are in the right place. Here, Creating a basic example of how to convert list to a string with whitespace in Python.
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 the list to string spaces separated. so let's see the below examples.
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
#Python