Python List to String – Definitive Guide

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

In python, you can convert the list to String using the String join() method.

You can create a list using square brackets as below.

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

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 items in a list to single 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 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. In the print statement, string[:-1] means, it’ll slice the string from the beginning to the character before the last element. -1 means to slice out the last character.

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.

Convert List to Multiline String

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

You can do this by the new line character \n as the separator. You can use the function in the previous section and change the delimiter character to \n to convert the list to a multiline string.

In this String object, the Slice operator will slice out the last line.

Example

print(listToString(output_str,'\n')[:-1]) 

Output

Stack
Vidhya
Definitive
Full
Stack
Tutorials

This is how you can convert a Python list to a multiline String.

Convert List to Tab Delimited String

In this section, you’ll learn how to convert a list to a tab-delimited string. This is also called a tab-separated string.

You can do this by the tab character \t as the separator. You can use the function in the previous section and change the delimiter to \t to convert the list to a tab-delimited String.

In this String object, the slice operator will slice out the last tab space.

Example

print(listToString(output_str,'\t')[:-1]) 

Output

Stack    Vidhya  Definitive  Full    Stack   Tutorials

Convert List to Pipe Delimited String

In this section, you’ll learn how to convert a list to pipe delimited string. | is known as the pipe symbol and the string will be delimited using | character.

You can do this by using the pipe character | as the separator. You can use the function in the previous section and change the delimiter to | to convert the list to a pipe-delimited String.

In this String object, the slice operator will slice out the last pipe symbol.

Example

print(listToString(output_str,'|')[:-1]) 

Output

Stack|Vidhya|Definitive|Full|Stack|Tutorials

This is how you can convert a list to a string with various separators using the for loop.

Next, you’ll learn the join() method.

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.

Next, you’ll learn how to convert a list to JSON String.

Convert List to json String

In this section, you’ll learn how to convert a list to JSON String.

JSON is a Javascript object notation where a data is stored as a key and value pair separated by a colon :. Each key-value pair is separated using the comma ,.

To convert a list to a JSON string, you need to import the json library.

Then you can use json.dumps() to convert the list to JSON String. Then you can use the index to access the item in the JSON String.

Example

import json

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

jsonStr = json.dumps(output_str)

print(jsonStr)

Output

["Stack", "Vidhya", "Definitive", "Full", "Stack", "Tutorials"]

This is how you can convert a list to a JSON String.

Conclusion

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

You’ve learned the different methods available to convert items in a list into a single 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

Python Add To Dictionary.

Leave a Comment