Python Certification Training for Data Scienc ...
- 50k Enrolled Learners
- Live Class
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:
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)
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)
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
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)
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
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))
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(''))
Or this one, if your objects contain square brackets:
list_of_ints = [1000,154,4528,8457] print(str(list_of_ints)[1:-1])
Finally, you may use map() to convert each item in the list to a string, and then join them:
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)
Let’s look into writing 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()
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()
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.