EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Python String Tutorial Python List to String

Python List to String

Updated April 18, 2023

Python List to String

Introduction to Python List to String

In Python, lists are similar to arrays and can have elements of any data type. List in simple words is a collection of elements in a sequenced order, with each element having indexes. Strings in Python are data types like other data types such as int, char, etc., and string, in general, can be defined as a sequence of characters. So in this article, we will see how to convert lists into strings in the Python programming language, and there are several other ways of doing this.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Convert List to String with Examples

In this article, we introduced to what are lists and string in Python. Now let us see how to convert lists to string in Python. There are different methods to convert lists to string in Python.

Let us see each method with examples below:

1. List to String

As the name itself suggests, this method is used when we have created an empty string, then we can convert the list to string by just iterating through the list and adding the elements respective to the index in this empty string, and this can be shown in the below example.

Code:

print("Program to convert a list to string:")
print("\n")
def func(s):
    str1 = ""
    for e in s:
        str1 += e
        return str1
s = ['Educba', 'Training', 'Institute']
print("The given list is as follows:")
print(s)
print("\n")
print("The given list is converted to string as follows:")
print(func(s))

Output:

Python List to String 1

In the above program, we can see we have defined a function func() in which we have passed a list named as “s” inside the function we have initialized an empty string “str1” then using “for” loop, we traverse through the string and return the string by converting the list that was passed to this function and then the output gives the string by combining all the elements of the given list.

2. By Using .join() Method

In Python, there is a method for string known as the join() method, and this method can also be used to convert the given list to string.

Syntax:

str.join(iterable)

Here iterable can be a list, set, tuple, etc.

Code:

print("Program to convert a list to string using join() function:")
print("\n")
def func(s):
    str1 = " "
    return (str1.join(s))
s = ['Educba', 'Training', 'Institute']
print("The given list is as follows:")
print(s)
print("\n")
print("The given list is converted to string as follows:")
print(func(s))

Output:

using .join() Method

In the above program, we can see a defined function “func()” where we have passed a list “s”; this function is for conversion. Firstly in this function, we initialize an empty string and then, using the join() method, we convert the given list to a string. One thing to note is that using the join() method will append space after every element of the list and then joins the list elements to one single string.

3. Using List Comprehension

There might occur a situation where a list can contain string and integers, then how to convert such lists to a string? So we need to add string while adding to a string. This can be done using this method.

Code:

print("Program to convert a list to string using list comprehension:")
print("\n")
s = ['Educba', 'Training', 'has', '10', 'courses', 'with', '2', 'subcourses', 'for', 'each', 'course']
print("The given list is as follows:")
print(s)
print("\n")
listToString = ' '.join([str(e) for e in s])
print("The given list is converted to string is as follows:")
print(listToString)

Output:

Python List to String 3

In the above program, we can see we have a list defined which has elements as both sting and integers. So to convert these kinds of lists, we use this comprehension method where we use both join() method and “for” loop in one single statement and print the entire elements of lists as a string. The output is as shown in the screenshot.

4. Using map() Method

This method is used for converting the list to string for mapping string with the given list. This method is used when the list has nonstring elements where these elements are converted to a string first. We have seen the above example also doing the same work but using the map() method.

Code:

print("Program to convert a list to string using list comprehension:")
print("\n")
s = ['Educba', 'Training', 'has', '10', 'courses', 'with', '2', 'subcourse','for', 'each']
print("The given list is as follows:")
print(s)
print("\n")
listToString = ' '.join(map(str, s))
print("The given list is converted to string is as follows:")
print(listToString)

Output:

map() Method

In the above program, we can see the same working as the program using the comprehension method, but here we use the map() function to convert the list into a string, and the result is as shown in the screenshot above.

Conclusion

In this article, we conclude that the conversion of the list to string is simple in Python. In this article, we saw various methods for converting the given list to a string. At first, we saw by adding elements to the empty string from the given list; second, we saw the use of join method for conversion; third, we saw using comprehension method using join() and lastly, we saw the same comprehension method with the same comprehension map() method.

Recommended Articles

We hope that this EDUCBA information on “Python List to String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Python pip
  2. Python json.dumps
  3. Python Parser
  4. Python Unicode Error
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
PYTHON for Machine Learning Course Bundle - 39 Courses in 1 | 6 Mock Tests
 125+ Hour of HD Videos
39 Courses
6 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.8
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more