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 :
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 :
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. check more on python doc
- Python program to find the largest even and odd numbers in a list
- Python program to sort values of one list using second list
- Python program to find the maximum and minimum element in a list
- Python program to find the multiplication of all elements in a list
- Python program to remove an element from a list using ‘del’ statement
- Python program to merge two lists and sort it