Converting Python List to String

To convert a list to a string in Python, you can use the string.join() method, which takes a list (or iterable) as an argument and returns a new string. The list must contain only strings; otherwise, string.join() will throw a TypeError exception. If you don't have a homogeneous list (a list containing elements of the same type), you can use the Python map() method to make a list homogeneous and then use the string.join() method to convert it to a string. The map(str, my_list) method iterates over all list elements and converts them to a string. You can also convert a list to a string by manually looping through the list and converting the elements of the list to a string. In this Python List to String example, we use the string.join() method which concatenates the list elements into a string and returns it as output. Below you can see more examples of converting Python lists to strings with a detailed description of each method. Click Execute to run the Python List to String Conversion Example online and see the result.
Converting Python List to String Execute
my_list = ['I', 'like', 'Python'] 

print(" ".join(my_list)) 
Updated: Viewed: 3276 times

What is Python?

The Python is one of the most popular general-purpose programming languages, used for server-side programming, application testing, machine learning, and as "glue code" to connect various components, written in different programming languages. Python has dynamic typing and dynamic binding, an interpreted and object-oriented language, and an integrated garbage collector. Almost all operating systems, including Windows, Linux, and macOS, support Python programming. Python is easy to learn, and code written in Python is easy to maintain. Python has built-in modules for working with HTTP requests, JSON, strings, and XML out of the box.

What is the List in Python?

Python lists are a collection of objects that allow you to organize and store data. Lists can contain items of different types of data, such as numbers, strings, classes, and even other lists. Lists in Python are dynamic structures; you can add, remove, or sort lists "in place" using list manipulation methods. You can create a list and initialize it with values enclosing the values in square brackets, separated by commas.

Create and initialize Python list
my_list = [1, 3, 2, 4]

print(my_list)

# [1, 3, 2, 4]

How to convert a list of strings to a string in Python?

The string.join() method takes an iterable, such as a list or tuple, as a parameter and returns a new string containing the elements that were joined together using the specified delimiter.

Python List join() Syntax
string.join(iterable)

Where
  • string: the separator
  • iterable: any iterable object - list, tuple, set, etc.

The following is an example of converting a list to a string in Python:

Convert Python list to string using the string.join()
my_list = ['I', 'like', 'Python'] 

print(" ".join(my_list))

# I like Python

How to convert a list of strings and numbers to a string in Python?

If you convert a non-homogeneous Python list using the string.join() method, you will get a TypeError exception. To convert such a list to a string, you must first convert it to a homogeneous list. To do this, you can use the map(str, my_list) method. The map() method takes a transform function as its first argument and an iterable object such as a list or tuple as its second argument. The map() method applies a transform function to each list element and returns a new list as a result.

Python List map() Syntax
map(function, iterable)

Where:
  • function: the specific function you want to execute on each list item
  • iterable: an iterable containing values

The following is an example of converting a list of strings and numbers to a string in Python:

Convert Python list to string using the list.map()
my_list = ["My", "name", "is", "Steve,", "I'm", 23, "years", "old"]

new_list = map(str, my_list)

print(' '.join(new_list))  

# My name is Steve, I'm 23 years old

How to convert a list to a string using a for loop in Python?

To convert a list to a string using a for loop, you can loop through the list and concatenate each list item to a string. Unlike the string.join() and map() methods, you can perform additional conditional checks and conversions. The following is an example of converting a list to a string using a for loop in Python:

Convert Python list to string using the "for" loop
my_list = ['Jack ', 'Leo ', 'Alice']

my_string = ""

for x in my_list:
  my_string += x

print(my_string)

# Jack Leo Alice

See also