Convert List To String In Python

In this post, We will see how to convert list to string in python. So without further ado let’s get started.

1. Convert List To String In Python By Using join() Method

The string.join(iterable) takes an iterable as an argument and join all its elements by a string separator.

In this code example, we used join() method of the string data type to concatenate the list of elements to the string.

# program to convert list to string in python
def converter(array):
    # Function to perform conversion

    # Join the all elements by the space character
    # note : it will also work for other data types.
    return ' '.join(str(i) for i in array)

list1 = ['the','world','is','beautiful']
list2 = ['it','contains','ints',1,2.5]

print(converter(list1))
print(converter(list2))

Save and run the above code you should see the following output.

Output :

output of program to convert list to string in python

2. By Using For-loop

In the following code snippet, we used for loop to convert list to string. here is the algorithm.

  • We first initialize an empty string.
  • Then we iterate through each element of the input list.
  • On each iteration, we convert the element to string and add it to the final string along with the white-space character.
def converter(array):

    final = '' # initialize an empty string

    for i in array: # loop through array
        # convert the element to string
        si = str(i)
        # add string to final along with space character
        final += si + ' '

    return final  # return the final string to the caller


list1 = ['the','world','is','beautiful']
list2 = ['it','contains','ints',1,2.5]


print(converter(list1))
print(converter(list2))

That’s the wrap for this tutorial.

Also read about how to convert string to list in python.

Leave a Reply