
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can we convert a list of characters into a 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 few ways to convert a list of characters into a string in python.
Using the 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 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
Using join() and map()
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
Using loop
We can convert a list into a string using a loop.
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
- Related Articles
- How can we convert a list of characters into a string in Python?
- How to convert a list of characters into a string in C#?
- Java program to convert a list of characters into a string
- C# program to convert a list of characters into a string
- Convert a string representation of list into list in Python
- Convert a String to a List of Characters in Java
- How to convert an array of characters into a string in C#?
- Convert list of string into sorted list of integer in Python
- Convert a String into a square matrix grid of characters in C++
- How to remove a list of characters in string in Python?
- How to convert a list into a tuple in Python?
- Convert List of Characters to String in Java
- How we can convert Python objects into JSON objects?
- Convert list of strings and characters to list of characters in Python
- Python Program to convert the array of characters into the string
- Convert set into a list in Python
