Python List to String

Share:

Date:

Categories:

One of the most basic data structure of the Python language is a List or a sequence.

A List is a collection of elements which is assigned by a number that is its index or position.

Python List is a zero-indexed data structure.

Python list is a very versatile datatype in which we can assign different types of elements separated by commas.

In this article, I’ll show you different ways to easily convert a python List to String.

Convert a list of string with join

If you have a List with elements of string type you can easily convert the entire list to string with the join() method.

Let us take the following example

>>> list = ['This', 'is', 'a', 'python', 'list']
>>> list_to_string = " ".join(list)

>>> print(list_to_string)

>>> This is a python list

>>> type(list_of_string)
>>> <class 'str'>

As you can see we took a list with 5 elements and using the join() method we converted them into a string.

Convert a list of int, float, and string with join

As I mentioned a list can consist of different types of element, let us assume you have a list with different datatype other than string like int and float, the join() method would not work as it would result in an error.

To avoid such a problem we can use the following ways

Convert a list with join and for loop

>>> list_with_mix_types = ['This', 'is', 1, 'list', 'with', 'mixed', 'types', 2.0]

>>> list_to_string = " ".join(str(x) for x in list_with_mix_types)

>>> print("Result :", list_to_string)

>>> Result : This is 1 list with mixed types 2.0

>>> type(list_of_string)

>>> <class 'str'>

Convert a list with join and map

Besides using a for loop you can also use the Python map function to loop through the list and convert the List to String.

>>> list_with_mix_types = ['This', 'is', 1, 'list', 'with', 'mixed', 'types', 2.0]

>>> list_to_string = " ".join(map(str, list_with_mix_types))

>>> print("Result: ", list_to_string)

>>> Result:  This is 1 list with mixed types 2.0

>>> type(list_of_string)

>>> <class 'str'>

Example to convert python list to string

Save the following code in a file called list_to_string.py

def main():
  """
  converts a list to string
  """

  # List of strings
  string_list = ["This", "is", "a", "list", "with", "strings"]

  # List with mixed data types
  mixed_list = ["Mixed", "list", "with", 1, "int", "and", "floats", 1.0, 2.0]

  # converts a list to string with join() method
  list_to_string = " ".join(string_list)  

  # prints the list converted to string
  print("String list: ")
  print(list_to_string)

  # Loops through a list using map function
  mixed_list_to_string = " ".join(map(str, mixed_list))  

  # prints the list converted to string
  print("Mixed list: ")
  print(mixed_list_to_string)

if __name__ == '__main__':
  main()

Run the file in the terminal with python list_to_string.py

python list_to_string.py

String list:
This is a list with strings
Mixed list:
Mixed list with 1 int and floats 1.0 2.0

conclusion

As you saw there are different ways to convert a Python List to String depending on the elements in it.

If you find this article helpful please share it, if you have any query please leave a comment below.