Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How To Convert List To String In Python?

Last updated on Sep 18,2020 11.8K Views

4 / 11 Blog from Python Programs

In our daily routine, we may come across a situation where we need to write something either to the command line, text file or a CSV file. The best way to do that is by using Strings. But many of us use data structures such as lists in our program to store some values or text in an iterative manner where we can add on different items to it. So, in order to write list items to a text file, we need to convert the lists to strings. In this article, we are going to learn about the conversion of lists to strings.

The topics we are going to discuss in this article are:

Let’s begin!

What are Lists in Python?

Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain Data Types like Integers, Strings, as well as Objects or even another list inside a list. Lists are also very useful for implementing stacks and queues. Lists are mutable, and hence, they can be altered even after their creation.

In Python, the list is a type of container in Data Structures, which is used to store multiple data at the same time. Unlike Sets, the list in Python is ordered and has a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index.

Lists in Python can be created by just placing the sequence inside the square brackets[]. A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.

mylist = ["This","is","a","Sample","Program","in","Edureka"]
mylist
type(mylist)

Output:

Output 1- Conversion of list to strings- Edureka

Now let’s look into what is strings

What is the String in Python?

In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. Strings in Python can be created using single quotes or double quotes or even triple quotes

my_string = 'Sunil'
my_string
type(my_string)

Output:

Output - Conversion of list to strings- Edureka

String in single quotes cannot hold any other single-quoted character in it otherwise an error arises because the compiler won’t recognize where to start and end the string.

my_string = 'Bayes'Theorem'
my_string

Output:

Output - Conversion of list to strings- Edureka

To overcome this error, the use of double quotes is preferred, because it helps in the creation of Strings with single quotes in them.

my_string = "Bayes'Theorem"
my_string
type(my_string)

Output:

Output - Conversion of list to strings- Edureka

For strings that contain Double quoted words in them, the use of triple quotes is suggested. Along with this, triple quotes also allow the creation of multi-line strings.

my_string = '''My friend said edureka courses are "fantastic" to learn.'''
my_string

Output:

Output - Conversion of list to strings- Edureka

Now moving on with convert list to strings

How to convert List to String in Python?

There are a few useful tips to convert a Python list to a string for display or writing it to any format of a file.

i) Consider list of strings, you may simply use join method this way:

mylist =["edureka","python","blog"]
print(",".join(mylist))
mylist =["edureka","python","blog"]
print("n".join(mylist))

Output:

Output - Conversion of list to strings- Edureka

However, this simple method does not work if the list contains non-string objects, such as integers.

ii) Consider list of integers and you want to obtain a comma-separated string, you may use this shortcut:

list_of_ints = [1000,154,4528,8457]
print(str(list_of_ints).strip('[]'))

Output:

Or this one, if your objects contain square brackets:

list_of_ints = [1000,154,4528,8457]
print(str(list_of_ints)[1:-1])

Output:

Output - Conversion of list to strings- Edureka

Finally, you may use map() to convert each item in the list to a string, and then join them:

print(",".join(map(str,list_of_ints)))
print("n".join(map(str,list_of_ints)))

Output:

Output - Conversion of list to strings- Edureka

iii) Consider a list containing both integers and strings, you can follow the below way:

string_int_list = ["This","is","a","Sample","Program","in","Edureka"]
str_out = ' '.join([str(elem) for elem in string_int_list])
print("Joined String : ",str_out)

Output:

Output - Conversion of list to strings- Edureka

Let’s look into writing a list of items into a text or CSV file

How to write a list of items into a text or CSV file?

To write data in a file, the Python programming language offers the standard method write() for dealing with a single line, as well as writelines() for dealing with multiple lines. Let’s see how we can write a list of items into a file:

string_int_list = ["This","is","a","Sample","Program","in","Edureka"]
file1 = open("output1.txt","w")
file1.write(' '.join([str(elem) for elem in string_int_list]))
file1.close()

Output:

If you want to write it to any other format just change the extension of the output file and you will get the desired result.
You can use the “n” which tells the program to break after each item in a list:

string_int_list = ["This","is","a","Sample","Program","in","Edureka"]
file1 = open("output1.txt","w")
file1.write('n '.join([str(elem) for elem in string_int_list]))
file1.close()

Output:

These are the ways you can convert a list to strings and write them to any file format. Python helps you with a really friendly environment in order to implement all these steps which might be somewhat longer if you are planning in any other programming language.

This brings us to the end of this article where we have learned how we can convert list to strings. I hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Conversion Of Lists To Strings” relevant, check out the Edureka Python Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.

If you come across any questions, feel free to ask all your questions in the comments section of “Conversion of Lists to Strings” and our team will be glad to answer.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 25th January,2025

25th January

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Convert List To String In Python?

edureka.co