How to Convert Python List to String – Definitive Guide

Python Lists are used to store multiple items in one variable. Lists are ordered, and changeable and it also allows duplicate values.

You can convert the python list to String using the String join() method.

Lists can also hold values of different data types. For example, a list can contain both String and Integer values.

s = ['Stack','Vidhya','-', 'No','-',1,'Definitive','Full','Stack','Tutorials']

With this list of values, you may need to convert Python list to string for printing or any other usage in your program.

If You’re in Hurry…

You can convert python list to String using the join() method as shown below.

output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

str = ' '.join(output_str)

print(str)

Output

Stack Vidhya Definitive Full Stack Tutorials

You can use any separator in the place of ' ', if you want to convert list to String with a separator instead of space.

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to convert items in a list to a single string in Python.

Using For Loop

In this section, you’ll learn how to convert a list to a string using for loop.

Using for loop, you’ll iterate every item in the list and then concatenate each item into the string variable str using the concatenation operator +=.

Example

# Define Function to convert  list to string
def listToString(stringaslist): 

    # initialize an empty string
    str = "" 

    # traverse the list and concatenate to String variable
    for element in stringaslist: 
        str += element  

    # return string  
    return str 

#Input List as String  
list_with_string =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

print(listToString(list_with_string)) 

Output

StackVidhyaDefinitiveFullStackTutorials

In the above example, you have not used any separator to concatenate the Strings.

You can use this method if you want to convert list to String without join().

In the below subsections, you’ll see how you can use the separators.

List to String With comma separated

In this example, you’ll learn how to convert a list to a string with commas.

The function accepts a delimiter character which can be used as a separator.

You can use any characters such as commas- , , quotes-‘' or newline characters such as \n.

While using the for loop to iterate each element, you’ll also concatenate the separator character. Hence, the resultant string will be a comma-separated value, if you use , as a separator.

This method will concatenate the separator at the end of the last string object as well. You can use the slice operator [] to slice out the separator at the end of the concatenated String.

Example

# Python program to convertlist to string with separator

def listToString(stringaslist,delimitor): 

    # initialize an empty string
    str = "" 


    # traverse the list and concatenate to String variable
    for element in stringaslist: 
        str += (element + delimitor)   

    # return string  
    return str_array 

#Input List as String  
output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

# Using the slice notation, removing the appended comma at the end
print(listToString(output_str,',')[:-1]) 

Output

Stack,Vidhya,Definitive,Full,Stack,Tutorials

This is how you can concatenate a list as a string with a separator.

Using Join() Method

In this section, you’ll learn how to convert list to string using join() method.

Join method takes all the items from an iterable and joins them into one String object. You can pass the list with the string objects to the Join() method and it’ll return a list.

Snippet

output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

str = ' '.join(output_str)

print(str)

You can also use the join() method to convert list to a comma-separated string or any other character delimited String.

Join() method joins the String with the passed delimiter character.

In the below example, , is used as the separator, hence you’ll see the comma-separated string.

Example

def listToString(stringaslist, separator): 

    # initialize an empty string
    str = "" 

    str = separator.join(stringaslist)

    # return string
    return str

#Input List as String  
output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

print(listToString(output_str,",")) 

Output

Stack,Vidhya,Definitive,Full,Stack,Tutorials

This is how you can use the join() method.

Next, you’ll see how to use the list comprehension method.

Using List Comprehension

In this section, you’ll learn how to convert a list to a string using list comprehension.

List comprehension allows you to create a new list based on the values of an existing list based on a specific condition.

Basically, it iterates the list and each element will be converted into a String and joined into a single string.

Snippet

stringaslist =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

 str = separator.join([str(elem) for elem in stringaslist])

Example

The below example uses the list comprehension and joins the list items to String. The separator variable is used to delimit the String.

#List to string using List Comprehension

def listToString(stringaslist, separator): 

    # initialize an empty string
    str = "" 

    str = separator.join([str(elem) for elem in stringaslist])

    # return string
    return str

#Input List as String  
output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

print(listToString(output_str,",")) 

Output

Stack,Vidhya,Definitive,Full,Stack,Tutorials

This is how you can convert list to String using List comprehension.

Next, you’ll learn how to use Map().

Using Map() Method

In this section, you’ll learn how to convert list to string using map().

Map executes a specified function to each item in an iterable.

Basically, it iterates the list and each element will be sent to the function as an input parameter.

In this method, each item in the list will be passed to the join() function which will join the list items into a String object.

Snippet

stringaslist =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

str = separator.join(map(str, stringaslist))

Example

The below example uses the map() function and joins the items in the List to String. The separator variable is used to delimit the String.

# List to string using Map

def listToString(stringaslist, separator): 

    # initialize an empty string
    str = "" 

    str = separator.join(map(str, stringaslist))

    # return string
    return str_array


#Input List as String  
output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

print(listToString(output_str,",")) 

Output

Stack,Vidhya,Definitive,Full,Stack,Tutorials

This is how you can convert python list to String using the map() function.

Next, you’ll use the str() function.

Using str() Method

In this section, you’ll learn how to convert list to String using the str() method.

str() method converts the specified value into the String. If you specify a list of String, then it’ll convert it into a single String object.

# List to string using STR() method

def listToString(stringaslist, separator): 

    # initialize an empty string
    str_array = "" 

    str =str(stringaslist).strip('['',]')

    # return string
    return str


#Input List as String  
output_str =['Stack','Vidhya','Definitive','Full','Stack','Tutorials']

print(listToString(output_str,",")) 

Output

'Stack', 'Vidhya', 'Definitive', 'Full', 'Stack', 'Tutorials'

In this, you can convert list to String by removing brackets. For removing brackets, you can strip() the brackets [ ] from the String object that is created.

Conclusion

To summarize, converting python List to String is one of the commonly used functionality.

You’ve learned how to convert python list to string with different delimiters and a multiline String or a Tab-separated String.

Using the JOIN() method is the recommended method to convert the List to String. Because the other methods also use the join() to concatenate the different items into one string.

If you’ve any feedback, feel free to comment below.

You May Also Like

Leave a Comment