List to String Python

In this article, we are going to use various built-in functions smartly to convert a python list to a string.  But before getting the start, let us clarify what is List and string?  Table of …

List to String Python

In this article, we are going to use various built-in functions smartly to convert a python list to a string. 

But before getting the start, let us clarify what is List and string? 

What is a List in Python?

  • A python list is a data structure that stores all data types in a sequence. 
  • Item of the list can be duplicated or redundant. 
  • Lists in python are mutable that are defined inside square brackets as discusses in List.
a = ["Python", "is", "an", "easy", "syntax", "language"]
print(a)

Output

List to String Python

To convert python lists to strings of characters, we will understand the concept of what string is.

What is String in Python?

A String in python is an immutable data type (unchangeable) of characters sequence. It is simple text that is readable.

For example, Python is an easy syntax language.

The list may contain different data types that cannot convert and print strings with direct methods. We use different functions to convert python lists to strings when the list contains both characters and integers.

This is the article about how to convert a list to string in python. There are various methods, and we are going to discuss 6 distinct methods of converting python lists to strings.

  1. Using * operator 
  2. join method 
  3. Map function
  4. for loop
  5. List Comprehension (for loop with sep operator)
  6. Translate method

1. Using * and sep operator to Print python list as a string.

* Operator prints output as all the items of a list into a string.

a = ["She", "backed", 1, "chocolate", "&",3,"cheese","cakes"]
print(*a)

Output

Print Python List to string with comma

  • To convert python list to string with a comma, define comma inside sep method with * operator.
a = ["She", "backed", 1, "chocolate", "&",3,"cheese","cakes"]
print(*a, sep=",")

Output

List to String Python

2. Join Method to convert list to string python

  • Join method concatenate a list of items with defined space
  • It prints a python list that is separated with defined space.
a = ["Python","is", "a","general-purpose","programming","language"]
print(' '.join(a))

Output

List to String Python
  • Covert list to a comma-separated string, use “,” instead of white space in the join method.

Note: Convert the python list to string using the join method is incompatible when the list contains integers or other data types. To print other data types list as a string, we use the map method.

3. Map Function to convert python list to string

Map function used to convert python heterogeneous (mix data types) list into a defined data type.

  • Map function converts all list item that is characters and integers into string data type and then prints it.
a = ["She", "bought", 2, "cats", "in",550, "dollars"]
print(' '.join(map(str,a)))

Output

List to String Python

4. For loop to convert list to string in python

  • Define the list of items.
  • Define a for loop str1 that will execute each item of the list1.
  • print the converted python list to string with each item end with defined white space.
list1 = ["She", "bought", 2, "cats", "in",550, "dollars"]
for str1 in list1:
 print(str1 , end = " ")

Output

List to String Python

5. List Comprehension method to print python list to string.

  • The list Comprehension method iterates each list element over sequence.
  • Join method concatenate the list into the string and print it.
b = ["She", "bought", 2 , "cats", "in",550, "dollars"]
X = ' '.join( str(a) for a in b)
print(X)

Output

List to String Python

6. Translate Method to Print list as string Python

  • Use ASCII code of open and close square brackets, double quotation, and comma.
  • Set the above-mentioned values as None.
a= ["She", "bought", 2 , "cats", "in",550, "dollars"]
rep = {39: None, 91: None , 93: None, 44:None}
print(str(a).translate(rep))
List to String Python

Convert the list of lists to string Python

These are the easiest steps to convert a list of lists into the string 

  • Define the lists of characters inside a list
  • Concatenate the list of characters with the join method
  • Print the result as a string and separate two lists of characters with space
a= [["D", "r", "a", "s","t","i","c"],["C", "o", "d", "e"]]
res = list(map(''.join, a))
print(*res, sep=" ")

Output

List to String Python

Conclusion

This article helps you how can you use various functions to convert python lists to strings. We use six understandable techniques to convert python list to string.

Suggested Read: Remove Brackets from list Python

Leave a Comment