Convert list to string python using 3 methods with examples

The conversion of the list to string in python is very straightforward. We have curated this guide with some examples of simple conversion tactics.

If you know the little bit part of programming languages such as c, c++, and java then you might be familiar with the array.

Arrays are the data structure that is used to store the elements of the same data type such as string, integers, and so on.

The list is also a data structure but its concept of storing the data is different from arrays. A list can contain the elements of different data types at the same time.

A list can also contain another list at the specified index.

For example :

  List_a = [‘happy’, [2,4,6,7,8],’coding’]

Now let’s see how we can convert a list into a string. There are so many methods in python which can be used to convert a list into a string.

[su_label type=”success”]Suggested read[/su_label] Determine length of list python

First method

The built-in .join() function in python is used to convert a list into a string. There are several methods for converting a list into a string but for now, let’s try to understand .join() first.

Example code :

 list_1 = [“hello”, “world”, “happy”, “coding”]
print(“ ”.join(list_1)) # space is used for separating the elements of the list

list_2 = [“new_york”, “central_city”, “gautham_city”]
print(“|n”.join(list_2))

Output :

hello world happy coding
new_york
central_city
gautham_city

Explanation of the code :

In the first line of the above code, we declare a list variable called list_1 that contains only the string elements. Then in the first print() function, we apply the .join() function onto the list_1.

list _2 is also containing the string elements and the .join() function will be applied to it in the print() function.

[su_label type=”success”]Suggested read[/su_label] Python string to int conversion

Second method

There is a problem with .join() function. You can not use the .join() function when your list contains integers. Then we can use the map() function along with .join() function.

The map() function is also a built-in function in python that is used along with the .join() function to convert the integer list into a string.

The map() function will map the integers list into a list of strings then join() function will convert the list of strings into a string that we saw in the above example.

Example code:

list_1 = [2,”plus”,2 ,”is”,4]
print(“ ”.join(map(str,list_1)))

Output :

2 plus 2 is 4

Explanation of code :

The first line of the above code contains a list named list_1 that contains a string and integer elements. The second line will contain map() function and .join() function.

map() function will map the integer elements in the form of string. Then the .join() function will convert it into a single string.

[su_label type=”success”]Suggested read[/su_label] How to sort a list in python

Third method

The str() function is also used to converting the list into a string.

Example code :

list_1 = [7,8,9,6,5,3,3,2,4,5,7,6,8,6,54,3,4,5,6]
print(str(list_1))
print(str(list_1)[1 : -1])
print(str(list_1).strip( ‘ [ ] ’ ))

Output :

[7,8,9,6,5,3,3,2,4,5,7,6,8,6,54,3,4,5,6]
7,8,9,6,5,3,3,2,4,5,7,6,8,6,54,3,4,5,6
7,8,9,6,5,3,3,2,4,5,7,6,8,6,54,3,4,5,6]

Explanation of code:

The first line of the above code contains a list of integers. Then we convert it into the string.

In the third line, we manipulate the str() function to get a string of the numbers.

The fourth line is optional with the help of strip() function you can also get the string of the numbers.

Conclusion :

We introduced you so many different ways of converting a list into a string. Whether the elements of the list are string type or integer type.

The .join() function is only responsible for converting a list that contains only string elements into a string. You can use the map() function with the .join() function for converting a list that contains integers too into a string.

Leave a Comment