In this tutorial, we’ll look at how to convert a python list to a string.
How to convert a python list to a string?
You can use the string join()
function in a list comprehension to easily create a string from the elements of the list. The following is the syntax:
s = ''.join(str(i) for i in ls)
Here, we iterate through the elements of the list ls
converting each to a string object using list comprehension and then join the elements into a string using the string join()
method. For more details on the join()
method, refer to this guide.
Examples
Let’s look at a few examples.
1. List of string to string
To get a single string from a list of strings, you can use the syntax above. But, since all the items in the list are already string, you don’t need to explicitly convert them into a string. See the example below.
ls = ['a', 'e', 'i', 'o', 'u']
s = ''.join(ls)
print(s)
Output:
aeiou
In the above example, you can see that we directly passed the list to the join()
function without converting each item to string. This was possible because the items in the list were already string.
2. List of integers to string
To get a string from a list of integers, you need to explicitly convert each list item to a string (for example, in a list comprehension) before applying the join()
function. See the example below.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
ls = [1, 2, 3, 4]
s = ''.join(str(i) for i in ls)
print(s)
print(type(s))
Output:
1234
<class 'str'>
In the above example, the string s
is created from the items of the list ls
containing integer items.
If you’re not sure about the types of items in the list, it’s better to convert them to string and then pass it to the join()
function which is used to join string objects.
3. List to string with each item on newline
You can use a custom string to join the items in the list. The above examples used an empty string to join the items but you use any other string like a whitespace ' '
, newline character '\n'
, etc as well. See the example below.
ls = [1, 2, 3, 4]
s = '\n'.join(str(i) for i in ls)
print(s)
Output:
1
2
3
4
Here, we use a newline character '\n'
to join the list. The resulting string has each item of the list separated by a newline character.
For more on the python string join()
function, refer to the python docs.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.