Converting List to String in Python

Here, I will discuss how a list can be converted to a string in Python. Python has many ways of converting a list data type to a string data type of Python.

Iterating through the List

A straightforward method will be to iterate through the list and concatenate each element to an empty string. First, you have to create a List, then an empty string to store the List values. Each element of the list is accessed through For loop and appended to the string using the += operator. Finally, the string is printed.

			

exampleList = ["Mercury”, "Venus", "Earth", "Mars","Jupiter"]

exampleString = ""

for word in exampleList:
    exampleString+=word

print("Converted String: ", exampleString)

				

Output:

Converted String: MercuryVenusEarthMarsJupiter

Using Join Method

The join method is used to concatenate multiple strings together into a single string. You can use it for conversion. First, you have to create a List, then call the join() method on an empty string and pass the list as an argument. The join() method returns a string containing the list values.

			

exampleList = ["Mercury", "Venus", "Earth", "Mars","Jupiter"]

exampleString = "".join(exampleList)

print("Converted String: ", exampleString)

				

Output:

Converted String: MercuryVenusEarthMarsJupiter

Using Map function

The Map function takes all list elements and passes them to a function. The map function can also achieve the goal of string conversion from a Python list. The map() function takes two parameters first str() function and the second one is a list to be converted. The str() function converts each list element to a string value returned.

			

exampleList = ["Mercury", "Venus", 3, "Earth", "Mars","Jupiter", 5]

exampleString = "".join(map(str,exampleList))

print("Converted String: ", exampleString)

				

Output:

Converted String: MercuryVenus3EarthMarsJupiter5