Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python List to String

Python List To String

In this tutorial, we’ll Python list to string conversion. A Python list serves the purpose of representing elements for manipulation. It basically represents a collection of homogeneous elements.

Python String also serves the purpose of the collection of elements in the form of characters as input.

The elements of the List can be converted to 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. Python List to String Using join() Method

Python join() method can be used to convert a List to String in Python.

The join() method accepts iterables as a parameter, such as Lists, Tuples, String, etc. Further, it returns a new string that contains the elements concatenated from the iterable as an argument.

Note: The mandatory condition for join() method is that the passed iterable should contain string elements. If the iterable contains an integer, it raises a TypeError exception.

Syntax:

string.join(iterable)

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 
out_str = " "
print("Converting list to string using join() method:\n")
print(out_str.join(inp_list)) 

In the above example, the join() method accepts the inp_list as a parameter and concatenates the elements of the list to the out_str and thus returns a string as output.

Output:

Converting list to string using join() method:

John Bran Grammy Norah

2. List Comprehension along with the join() method to convert Python List to String

Python List Comprehension 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.

Python List Comprehension along with join() method can be used to convert a list to a string. The list comprehension will traverse the elements element-wise and the join() method would concatenate the elements of the list to a new string and represent it as output.

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 

res = ' '.join([str(item) for item in inp_list]) 
print("Converting list to atring using List Comprehension:\n")
print(res) 

Output:

Converting list to atring using List Comprehension:

John Bran Grammy Norah

3. Python List to String conversion with map() function

Python’s map() function can be used to convert a list to a string.

The map() function accepts the function and iterable objects such as lists, tuples, string, etc. Taking it ahead, the map() function maps the elements of the iterable with the function provided.

Syntax:

map(function, iterable)

Example:

inp_list = ['John', 'Bran', 'Grammy', 'Norah'] 

res = ' '.join(map(str, inp_list)) 
print("Converting list to string using map() method:\n")
print(res) 

In the above snippet of code, map(str, inp_list) function accepts str function and inp_list as arguments. It maps every element of the input iterable( list) to the given function and returns the list of elements. Further, join() method is used to set the output into string form.

Output:

Converting list to string using map() method:

John Bran Grammy Norah

4. Iteration using for loop to convert Python List to String

In this technique, elements of the input list are iterated one by one and are added to a new empty string. Thus, converting a list to a string.

Example:

inp_str = ['John', 'Bran', 'Grammy'] 
st = "" 	
for x in inp_str:
  st += x

print(st)

Output:

JohnBranGrammy

Conversion of List of characters to a string

Even a set of characters in the form of a list can be converted to a string in the same manner as above. Here is an example to demonstrate the conversion of a list of characters to a string.

Example:

inp_str = ['J', 'o', 'u', 'r', 'n', 'a', 'l', 'd', 'e', 'v']
st = ""
for x in inp_str: 
  st += x

print(st)

Output:

Journaldev

Conclusion

Thus, in this article, we have studied the different techniques and methods of conversion of Python List to String.


References