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, in 
    print(" ".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 

Updated on: 11-May-2023

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements