
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert List to String in Python
A list is a data structure in Python that is a mutable or changeable ordered sequence of elements. A list's items are any elements or values that are contained within it. Lists are defined by having values inside square brackets [], just as strings are defined by characters inside quotations. They are used to store multiple items in a single variable.
In Python, strings are among the most widely used types. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example - var1 = 'Hello World!'
There are a few ways to convert a list of characters into a string in Python ?
Convert the List using join() method
Using the join() method, we can convert a list into a string. The join() method takes in iterables, joins them, and returns them as a string. However, the values in the iterable should be of the string data type.
Syntax
Following is the syntax of this function -
string.join(iterable)
Where an iterable is a sequence, collection, or an iterator object. We can send as many iterables as we like.
Example 1
In the following example, a list with different elements is initialized. Then, using the join() method, the elements are joined and printed as output.
list=['There', 'are' ,'many' , 'datatypes' , 'in' , 'python'] print(' '.join(list))
Output
This generates the following result -
There are many datatypes in python
Example 2
Let us try to use join() method on an iterable containing an integer value. It would return a type error.
list = ["There", "are", 6, "datatypes", "in", "python"] print(" ".join(list))
Output
This generates the following result -
Traceback (most recent call last): File "/home/cg/root/63398/main.py", line 2, inprint(" ".join(list)) TypeError: sequence item 2: expected str instance, int found
Convert the List using join() and map() Methods
The join() and map() methods can be used to convert the list which contains integer values into strings. The map() function executes a specified function for each item in an iterable. The item is sent to the function as a parameter.
Syntax
Following is the syntax of the map() method -
map(function, iterables)
Where,
-
function is the function to execute for each item.
-
iterables is a sequence, collection, or an iterator object. We can send as many iterables as we like.
Example
In the following example, a list with different elements is initialized. Then, using join() and map() methods, the elements are joined and printed as output.
list = ["There", "are", "6", "datatypes", "in", "python"] print(" ".join(map(str,list)))
Output
This generates the following result -
There are 6 datatypes in python
Converting List to string using a for Loop
We can convert a list into a string using a loop. To do so, we just need to iterate over each element in a list and concatenate it to a string.
Example
In the following example, a list with different elements is initialized. Then, using a for loop, the elements of the list are joined as a string and printed as output.
list = ["There", "are", 6, "datatypes","in", "python"] string = "" for i in list: string += str(i)+ " " print(string)
Output
This generates the following result -
There are 6 datatypes in python
Convert the List of Mixed Data Types using map()
Here we will create a mixed list which contains both characters and numbers, and then we will be going to convert it to a string where all characters are properly represented as a single sentence.
Example
In the following example a list with different elements is given. Then with the help of the map() method the elements will be joined -
list = ["There", "are", 6, "datatypes", "in", "python"] string = " ".join(map(str, list)) print(string)
Output
This generates the following result -
There are 6 datatypes in python