In this tutorial we are going to share with you simple methods of how you can convert list to string in python.
What are Lists in Python?
A list is a collection of objects that are ordered and changeable. It can contain a variety of data types such as integer, character, or float. You may have heard of an array in other programming languages, lists are similar to them in python.
Only difference between a list and an array is that list can store different datatypes whereas array can only store a similar data type.
The list is represented using square brackets(), and a comma(,) is used to separate two objects present in the list.
list = ["apple", 34, True, 40, "male"]
Also read: What are tuples in python? How are they different from lists?
What are Strings in Python?
Strings in python are an ordered sequence of characters. A string is a data type composed of multiple elements of the same data type i.e character.
They are surrounded by either single quotation marks(‘ ), or double quotation marks(“).
a = "Hello, World!" b = ' I am online'
How to convert list to string in python
A list can be converted into a string by either of the following methods:
- By using join() method
- By using List Comprehension
- Iterating using for loop
- By using map() method
1. Convert List to String Using join() Method
We can use the join() method to convert a list into a string. The main point of the join() method is that it can convert only those lists into a string that contains string as its elements. Apart from lists as iterables, this method can use tuples and strings as a parameter.
If the iterable contains an integer, it raises a TypeError exception.
Lets see by example below:
input_list = ['This', 'is', 'an', 'example'] output_string = " " print("Converting list to string using the join() method") print(output_string.join(input_list))
Converting list to string using the join() method This is an example
In the above example, we can see that each element in the list are a characters i.e string, so here we can use the join() function directly. Each element in the new string is separated with a single space.
What if the list contains a data type other than string i.e character. In this situation, the join() method can not be used. Here we can use other methods of conversion from a list to a string.
2. Using List Comprehension along with the join() method to convert Python List to String
List Comprehension in python creates a list of elements from an existing list. It further uses the for loop to traverse the items of the iterable in an element-wise pattern.
We can use List comprehension along with the join() function to convert list to string. The list comprehension will pass over the elements one by one, and the join() method will concatenate the list’s elements into a new string and return it as output.
input_list = ['A', 'list', 'conversion', 'example', 'number', 1] listToStr = ' '.join([str(item) for item in input_list]) print("Converting a list to a string using List Comprehension") print(listToStr)
Converting a list to a string using List Comprehension A list conversion example number 1
3. Iteration using for loop to convert Python List to String
In this method, elements in a list are iterated one by one and are added to a new empty string. Here an empty string has to be initialized to store the elements.
input_string = ['John', 'loves', 'coding'] str = "" for x in input_string: str += x print(str)
John loves coding
4. List to String conversion with map() function
The map() the function accepts the str() function and iterable sequence objects such as lists, tuples, string, etc. In the end, the join() function is used to combine all the values returned by the str() function.
The map function can be used if the list contains only numbers or If the list is heterogeneous meaning a combination of data types.
Lets see it by an example below.
input_list = ['Example', 'using', 'map', 'function', 1, 2, 3] result = ' '.join(map(str, input_list)) print("Converting a list to string using map() method") print(result)
Converting a list to string using map() method Example using map function 1 2 3
cool website, can you tell me what plugin you use for hihgliter syntax. looks very good.
We use Code Snippet block plugin