Python program to convert a list to string
Python program to convert a list to String :
In this example, we will learn how to convert a list to string. The list is one of the most commonly used datatype in python. List is used to storing a collection of different items. Python list is mutable, ordered and it can also hold duplicate values. In python, list is created by placing all items inside a square bracket (). Items are separated by commas. The index of list item starts at 0, i.e. the index of the first element is 0, the index of the second element is 1 etc. We can access any element in a list by using its index. We can also delete or update an element in a list.
The string is a collection of characters enclosed in quotes. We can use either single quotes or double quotes for creating a string. Even triple quotes can be used for creating a string in python.
In this tutorial, we will learn how to convert a python list to a string. The first thing that comes to our mind is to iterate through the list using a loop and concatenate each element to create the string. We can do that. But in this example, we will use the join method of String. join is defined as below :
The iterable passed to the join method should contain only string values. If it contains any non-string value, it will throw one TypeError. The value str is the separator string, i.e. the string that will be used to separate each element of the iterable. Let’s take a look into the program :
Python program :
dummy_list = ["one","two","three","four","five","six"] separator = ' ' result_string = separator.join(dummy_list) print(result_string)
The example programs are available here.
one two three four five six
Let’s try by changing the separator to ‘,’.
dummy_list = ["one","two","three","four","five","six"] separator = ',' result_string = separator.join(dummy_list) print(result_string)
Output is :
Now, if we change one item of the list to an integer, it will produce the following output :
Advantage and Disadvantage of join :
Converting a list to a string is one of the most commonly faced problems in python or any other programming language. For example, you have a list of string values and you need to store them in the database as a string, separated each one with a comma. join makes it easy for you.
The main advantage of the join method is its simplicity. If you have a list of string items and you need to convert that list to a string before doing any further processing, you can do it in a single line using join. Another advantage is that we can define custom separator in this method. Comma, colon or anything we can have as the separator of the final string.
The only disadvantage I can think of is that it doesn’t support all data types. In python, a list can hold different data types. But for join, we need all elements of the list to be of string type. Else, it will throw one TypeError.
My suggestion is that use ‘join’ if you are sure that all items in the list are of string type. Also, add more safety by enclosing it in a try-catch block.
Try to run the examples shown above and drop one comment below if you have anything in your mind.