Home » How to Convert a List to String in Python

How to Convert a List to String in Python

In this tutorial let’s learn about How to Convert a List to String in Python. Converting lists to strings is a common procedure in Python. Converting a list to a string is commonly used to show the contents of the list as a string or to do string manipulation.

Convert List to String using join() Method Python

The join() function in Python is used to concatenate a string with an iterable object. It returns the concatenation of the strings in iterable as a new string. The str data type is required as one of the parameters for the join method. If iterable contains a non-string value, it throws a TypeError exception.

Syntax for join():

string.join(iterable)

Code Example :

str_list = ["Similar", "Geeks", "Python"]

# join() method 
string = "".join(str_list)
print(string)

Output :

SimilarGeeksPython

The join() method concatenates any number of strings, inserting the string whose method is called between each given string. The string “”, an empty string, is added between the list elements, as seen in the example.

If you want a space ” ” to be added between the elements, use the below code sample.

Code Example :

str_list = ["Similar", "Geeks", "Python"]

# join() method - joining with space between them
string = " ".join(str_list)
print(string)

Output :

Similar Geeks Python

Convert List to String using map() and join() Methods in Python

If you are having list of integers other than strings then you can use this approach to convert the list to strings using join and map methods. In Python, this approach uses the map() and join() functions to convert lists to strings. When the iterable, in our instance a list, includes an integer value, this method is employed.

Because the join() methods accept only string values, we utilise the map() methods to transform the integer values to strings before converting the list to strings. The map() methods perform a specific function on each value in an iterable. In this case, we utilize it to transform each value in the list to a string.

Syntax for map()

map(function, iterable)

Code Example :

sample_list = ["Similar", 1 , 'Geeks', 23.4]

# join() & map() methods - joining elements with space between them
string = " ".join(map(str,sample_list))
print(string)

Output :

Similar 1 Geeks 23.4

You can remove the space between the words by giving the empty string in the join() method.

Convert List to String using List Comprehension Python

As previously stated, if your input list contains integer elements, the join() method will fail. To solve this problem, you can utilize the List comprehension in conjunction with the join() method. List comprehension will assist you in constructing a list of elements from an existing input list. Later, a for loop is used to traverse the elements using element-wise patterns. Following the traversal of items using list comprehension, you can concatenate the traversed elements of the list into an empty string using the join() method.

Code Example :

sample_list = ["Similar", 123 , 'Geeks', 456]

# list Comprehension
string = ' '.join([str(element) for element in sample_list]) 
print(string)

Output :

Similar 123 Geeks 456

Convert List to String using Loop in Python

The last method for converting lists to strings in Python is to loop through each object and append it to a string. This method is not recommended because Python generates a new string each time you append an object.

This method is slow and must only be used to help understand the concept better.

Code Example :

sample_list = ["Similar", 123 , 'Geeks', 456]

# using loop
string = ""
for i in sample_list:
    string += str(i)+ " " 

print(string)

Output :

Similar 123 Geeks 456

Conclusion

Lists and strings are both important data types in Python. This article discussed Python lists and strings in depth, as well as several procedures and methods for converting list data types to strings.

Similar Posts:

Check if a String Contains Substring in Python
Convert String to Datetime in Python