Как из list сделать str python
Given a list, write a Python program to convert the given list to string. There are various situations we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.
Example:
Let’s see various ways we can convert the list to string.
Method #1: Iterate through the list and keep adding the element for every index in some empty string.
Python3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using .join() method
Python3
But what if the list contains both string and integer as its element. In those cases, above code won’t work. We need to convert it to string while adding to string.
Method #3: Using list comprehension
Python3
The time complexity of the program is O(n), where n is the length of the list s, because it iterates over each element of the list once.
The auxiliary space complexity of the program is O(n), because it creates a new list of strings using a list comprehension.
Method #4: Using map() Use map() method for mapping str (for converting elements in list to string) with given iterator, the list.
Python3
The time complexity of this code is O(n), where n is the length of the list s.
The space complexity of this code is O(n), where n is the length of the list s.
Method #5: Using enumerate function
Python3
Method #6: Using in operator
Python3
Method #7: Using functools.reduce method
Python
The time complexity of the provided Python code is O(n), where n is the number of elements in the input list s.
The space complexity of the code is O(n), where n is the number of elements in the input list s.
Using str.format method:
One additional approach to convert a list to a string in Python is to use the str.format method. This method allows you to specify a string template, and then fill in the placeholder values with the elements in the list.
Python3
This approach has the advantage of being able to specify exactly how the elements in the list should be formatted, by using the formatting placeholders in the string template. For example, you can specify the number of decimal places for floating point numbers, or the width and alignment of the output string.
Python3
The time complexity of the above approaches will depend on the length of the list. For example, in method 1, we are iterating through the list and adding each element to a string, so the time complexity will be O(n), where n is the length of the list.
Similarly, the time complexity of other methods will also be O(n).
The space complexity of all the above methods will also be O(n) as we are creating a new string of size n to store the elements of the list.
Convert List to String in Python: 7 Best Methods (with code)

While working with Python data types, there are situations when you want to convert the data collected from one type to another. There are various methods by which we can convert a list to a string in Python with fewer lines of code. But before all that, let us understand what are lists and strings.
What is a List in Python?
A Python list is an ordered and changeable collection of data objects. It is written as commas-separated values inside the square bracket. It can also contain duplicate elements along with negative indexing elements.
The important advantage of the list is that the elements inside the list are not compulsory to be of the same data type. The list undergoes the operations like slicing, concentrating, etc., just like the string operations. Also, you can create a nested list, i.e., a list containing another list.
Example:
Output:
What is a String in Python?
A string is defined as a sequence of characters where a character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters therefore, it deals with binary numbers. Even though we can see characters on our monitor screens, internally it is stored as a combination of 0s and 1s.
In the Python programming language, a string is a sequence of Unicode characters. It s an immutable sequence data type wrapped inside single or double quotes. That means that once you define a string, you cannot change it.
You can also assign a multi-line string to a variable using triple quotes. Many string manipulation methods are available for string data types like join(), split(), concatenation, etc.
Example:
Output:
Why Convert the Python List to String?
Python provides a variety of functions and data types to work with. A list, which is an assortment of elements that may consist of any data type, is one of the most practical data types in Python. But occasionally we might need to change a list into a string.
First, if you want to display the contents of a list as a readable string, which is helpful when debugging or when you need to present data in a specific format. Second, sometimes, you may want to store a list in a database. Converting the list to a string allows you to write it to a file or store it in a database column that accepts strings.
There are various methods for turning a Python list into a string. We’ll look at three of the most widely used techniques as well: utilizing the enumerate() function, and using the functools.reduce() technique, and using the str.format() technique.
How to Convert List to String in Python?
As mentioned earlier, there are 7 ways to convert a list to a string in Python. Let us study them one by one in detail, along with the example and corresponding output:
1) Iterating through the List
This method will iterate every index of the input list one by one and add it to the empty string. This is the simplest method in Python to convert list to string.
Example:
Output:
2) Using join() method
Every element of the list will be traversed and added to the empty string. The join () method is used to concatenate all these elements and form the final output. This method will fail to work if the list contains integer elements, and therefore the output will be TypeError Exception.
Example:
Output:
3) Using List Comprehension
As mentioned earlier, the join() method will fail to work if you have integer elements in your input list. But to overcome this failure, you can use the List comprehension along with join() method.
List comprehension will help you to create a list of elements from the existing input list. And later uses a for loop to traverse the elements by element-wise patterns. After the traversal of elements by list comprehension, you can use the join() method to concatenate the traversed elements of the list into an empty string.
Example:
Output:
4) Using map() function
map() function accepts iterable objects such as tuples, lists, or strings. Hence, it is used to map the elements of the iterable objects with the function provided.
Example:
Output:
5) Using Enumerate Function
We may iterate over a list as well as its index at the exact same time using Python’s built-in enumerate() function. This function allows us to concatenate strings with a separator to create a single string from a list of strings.
Example:
Output:
In this example, we first build a string separator called separator as well as a list of strings called my_list. The result is then assigned to a variable called my_string after we connect the strings in the list using the separator using the join() technique. The my_string variable is then printed to the terminal.
A quick and effective approach to do this in Python is by combining the join() technique and the enumerate() function.
6) Using functools.reduce Method
Using the functions.reduce() method is another technique to translate a Python list into a string. With Python’s built-in reduce() method, we can apply a function across a list of elements and then receive one value back. This technique can be used to combine a list’s items into a string.
Example:
Output:
In this example, the reduce() method is imported first from the functools module. Then, we define a separator string named separator as well as a list of strings called my_list.
Applying a lambda function that joins two components together with the separator to the list using the reduce() method, we then assign the outcome to a variable called my_string. The my_string variable is then printed to the terminal.
employing functools.reduce() method allows more flexibility and control over the concatenation process, but it is a little more involved than using the enumerate() function.
7) Using str.format() Method
Python’s str.format() method is a potent mechanism for formatting strings that enables us to formattily insert values into strings. By adding a list into a string using the syntax, we can utilize this method to change a list into a string.
Example:
Output:
In the above example, a list called my_list was generated with three members, and it was then inserted into a string called my_string using the str.format() method. The result demonstrates that the list was transformed into a string by using square brackets.
Conclusion
List and string have their own importance as data types in Python. This article explains different techniques and methods to convert list data types into strings. It is highly recommended to learn all of them in detail as they are very effective to do the conversion using fewer lines of code.
Turn a list into a string in Python
Sign in to your Python Morsels account to save your screencast settings.
Don’t have an account yet? Sign up here.
Let’s talk about how to turn a list into a string in Python.
Using str to convert a list to a string
Let’s say we have a list of strings called things :
If we’d like to turn this list into a single string, we could pass it to the built-in str function:
But the output we get probably isn’t what we were looking for. At least not if we’re trying to pass this to an end-user rather than another programmer.
So we need to ask ourselves, what are we really trying to do here? Basically, what I’d like to do is join together each of the strings in this list by some kind of delimiter (like a space for example).
Converting a list to a string with join
Many programming languages have a join method that you can call on the list type or the array type, passing it a delimiter to join together all the strings in that list by a given delimiter. Python doesn’t have this!
In Python, our list type does not have a join method. Instead, our string type has a join method:
Here, we’ve asked the space character ( » » ) to kindly use itself as a delimiter, joining together the items in the list of strings we’ve passed to it.
Using a delimiter while joining
We can use any string as a delimiter. For example, using «, » as a delimiter would put a comma and a space between each of these strings:
Or an empty string ( «» ) would put nothing between each of them, concatenating them all (smooshing them together into one long word):
The string join method isn’t only for lists
Why does Python do it this way? Why is the join method on strings instead of the lists? This seems kind of backwards, right?
Python does it this way for the sake of flexibility.
In Python, we have lots of types of iterables (not just lists) and we tend to think in terms of duck typing (if it looks like a duck and quacks like a duck, it’s a duck). That is, we care about the behavior (e.g. iterability) of an object instead of its type (e.g. list).
Because the join method is on the string type, we can pass in any iterable of strings to it. For example we can join together a tuple of strings:
Or we can join together a file object (file objects are iterable in Python and when you loop over them you get lines):
We can also join together a generator object:
That generator expression removes new lines from the end of each line in a file and we’re using the string join method to join those lines together with a comma and a space.
Converting an iterable of non-strings into one string
What if the iterable that we’re joining together isn’t an iterable of strings? What if it’s an iterable of numbers?
When we try to join together an iterable of items that aren’t strings we’ll get a TypeError :
Because we know that the join method accepts any iterable of strings, we could write a generator expression that converts each of our items into a string by passing it to the built-in str function:
So even if you’re working with an iterable of non-strings, you can join them together as long as you do a little bit of pre-processing first.
Use the string join method to turn a list into a string in Python
Need to convert a list to a string in Python?
Just make a string for your delimiter and then call the join method on it:
This works on any iterable-of-strings, not just lists of strings.
What comes after Intro to Python?
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I’ll explain concepts that new Python programmers often overlook.
Series: Strings
Regardless of what you’re doing in Python, you almost certainly use strings all the time. A string is usually the default tool we reach for when we don’t have a more specific way to represent our data.
To track your progress on this Python Morsels topic trail, sign in or sign up.
How to Convert List to String in Python? Here are the 6 Ways to Know | Simplilearn
![]()
Python is one of the most popular programming languages today, and in this tutorial, we will learn various ways to change a list to a string. Apart from this, we will also discuss nuances of Python, including what is a list in python, what is a string, and more. Let’s start.
What Is a List in Python?
A list in python is an ordered sequence that can hold a variety of object types, such as, integer, character or float. A list in python is equivalent to an array in other programming languages. It is represented using square brackets, and a comma(,) is used to separate two objects present in the list.
A list and an array in other programming languages differ in the way that an array only stores a similar data type, meaning that an array is homogeneous in nature, but a list in python can store different data types at a time, and therefore it can either be homogeneous or heterogeneous. Below are some examples of homogeneous and heterogeneous lists in python:
Homogenous Lists:
Heterogeneous Lists:
Accessing an Item From the List
An item from the list can be accessed by referring to its index in the list. The indexing of elements in the list starts from 0. Let’s take an example of the list we created in the last step.
To access an element from the list, we pass the index of that element in the print function.
As mentioned earlier, that indexing starts from 0, so when index [1] is passed, it gives the result as “dog”. Similarly, if we pass the index, say [2], it will give the output 2.2
What Is a String in Python?
A string in python is an ordered sequence of characters. The point to be noted here is that a list is an ordered sequence of object types and a string is an ordered sequence of characters. This is the main difference between the two.
A sequence is a data type composed of multiple elements of the same data type, such as integer, float, character, etc. This means that a string is a subset of sequence data type, containing all elements as characters.
Here is an example of string in python and how to print it.
For declaring a string, we assign a variable to the string. Here, a is a variable assigned to the string simplilearn. An element of a string can be accessed in the same way as we saw in the case of a list. The indexing of elements in a string also starts from 0.
How to Convert a List to String in Python
The join function is one of the simplest methods to convert a list to a string in python. The main point to keep in mind while using this function is that the join function can convert only those lists into string that contains only string as its elements.
Refer to the example below.
Here, all the elements in the list are individual string, so we can use the join function directly. Note that each element in the new string is delimited with a single space.
Now, there may be a case when a list will contain elements of data type other than string. In this case, The join function can not be used directly. For a case like this, str() function will first be used to convert the other data type into a string and then further, the join function will be applied. Refer to the example given below to understand clearly.
- Traversal of a List Function
In this example, firstly we declare a list that has to be converted to a string. Then an empty string has to be initialized to store the elements. After that, each element of the list is traversed using a for loop, and for every index, the element would be added to the initialized string. At the end, the string will be printed using the print() function.
- Using map() Function
The map function can be used in 2 cases to convert a list to a string.
- if the list contains only numbers.
- If the list is heterogenous
The map() function will accept 2 arguments;
- str() function; that will convert the given data type into the string data type.
- An iterable sequence; each and every element in the sequence will be called by str() function. The string values will be returned through an iterator.
At the end, the join() function is used to combine all the values returned by the str() function.
- List Comprehension
List comprehension in python generates a list of elements from an existing list. It then employs the for loop to traverse the iterable objects in an element-wise pattern.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list’s elements into a new string and return it as output.
An example of conversion of list to string using list comprehension is given below.
- Iterating Through the List
In this method, Assign a list and traverse each element and add each into an empty string using for loop
The code in python is as given below
instr= [‘Hello’, ‘How’, ‘are’, ‘you’, ‘doing?’]
# passing in a string
The output will be — Hello How are you doing?
# listToString is a function
# initialize empty string
# string traversal using for loop
for ele in instr:
instr = [‘Hello’, ‘How’, ‘are’, ‘you’, ‘doing?’]
The output will be — Hello How are you doing?
The article explains two of the data types List,and String in Python language and some ways of converting List to String in Python programming language. All methods have code that has readability and functions that are used often to convert List into String. It is important to understand each of the functions and its use, the syntax used and the developer should have hands-on experience with the expected effect on output using these functions. In all these methods iteration is used to move from one element to the next element in a list and then add each of the elements and convert them as a single string.
In case you wish to master the A to Z of Python, enroll in our Python Certification Training today! And in case you have any questions regarding the tutorial, drop a comment below and our experts will help you out.