Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Readability counts.
This is the philosophy of Python programming language summarized in the document The Zen of Python.
Readability in any programming language is considered important as it helps developers code faster, clear, and in an efficient way. Python is consistently ranked as one of the most popular programming languages and used by a large number of organizations that include world-famous organizations such as Facebook, Amazon, Instagram, Spotify, CERN, NASA, Wikipedia, Google, Reddit, ILM, ITA, and many more.
Python offers simpler, less-cluttered, and concise syntax, making the language to be fun to use. This reflects even in simple programs like Python list to string.
In this article, we will see how to convert a list to string in Python.
There will be many situations where developers need to convert a list to a string python such as writing list items to a text file, CSV file, or command line. To do so, there are four ways to turn a list into a string Python
Here’s How We Can Change List To String Python
Before we see these four ways in detail, let’s see what are lists and strings in Python.
Lists are one of the most common data structures in Python. Lists are used to store multiple items in a single variable. It is one of 4 built-in data types in Python for storing data.
It may contain different data types such as integers, strings, objects, and lists inside a list. Lists are very useful and are a type of container in data structures. In Python, lists are ordered, this means, elements in a list are indexed as per definite count. The first element of a list is indexed at 0 and the second at 1.
List allows duplicate, changeable, and ordered values. Lists are widely used in implementing stacks and queues.
In Python, Lists can be created by placing the comma between the elements inside the square[] brackets. You can pass duplicate values because the positions of values will be different.
Example:
mylist = ["Hello", "world", "this", "is", "a", "list"]
print(mylist)
Output:
[‘Hello’, ‘world’, ‘this’, ‘is’, ‘a’, ‘list’]
Now, we will see what String is.
Strings are arrays of bytes that represent Unicode characters. Strings in Python are similar to many other programming languages. Python does not have a character data type. Strings in Python can be assigned using either single quotation marks or double quotation marks.
You can display string using the print() function. If you want to access elements of the string, you can simply use square brackets and index.
Example:
String1 = "Hello World"
print(String1)
Output:
Hello World
Now let’s see, how to convert list to string in Python.
join() accepts sequence like list, tuple, etc as an argument and then join all the items in an iterative sequence. 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 through a string.
However, while using join(), your values in the iterables should be of the string datatype.
Example:
name_list = ['Cristiano_', 'Ronaldo_', 'dos_', 'Santos_', 'Aveiro']
full_name = ""
full_name = full_name.join(name_list)
print(full_name)
Output:
Cristiano_Ronaldo_dos_Santos_Aveiro
You can add white space as a separator while printing or creating a function that does take any separator as an argument.
Also note that if you want to use join() for integer datatype, you need to convert a list of integers into a list of strings and then use join() to join all the items.
Map() takes two arguments. The first is a function and the second is an iterable sequence, i.e. list. It calls the given function on each element of the list and returns an iterator which iterates over the result object (string).
Example:
name_list = ['Cristiano', 'Ronaldo', 'dos', 'Santos', 'Aveiro']
#using map() function
full_name = ' '.join(map(str, name_list))
print(full_name)
Output
Cristiano Ronaldo dos Santos Aveiro
Here, str converts the given object into string values. The map calls str on each item and is returned via an iterator. Next, using the join() function, we have concatenated a string. Integers can also be used as list values if you are choosing this method to convert list to string.
You can use list comprehension and join() to convert list into string. We talked earlier that join() will not work if your list contains an integer value.
Here, list comprehension creates a list of elements from the given list and then uses a for loop to traverse the element one by one and then join() method will combine list’s elements into a string.
Example:
name_list = ['Cristiano_', 'Ronaldo_', 'dos_', 'Santos_', 'Aveiro']
#using list comprehension
full_name = ".join([str(name) for name in name_list])
print(full_name)
Output:
Cristiano_ Ronaldo_ dos_ Santos_ Aveiro
This is the easiest method to convert a list into a string.
Here “name_list” is a list containing the first, last, and middle names of Cristiano Ronaldo. Moreover “full_name” is currently an empty string where the names from the list will get joined into a string.
The list is traversed using a for loop and one by one, every element is added to the string.
Example:
name_list = ['Cristiano_', 'Ronaldo_', 'dos_', 'Santos_', 'Aveiro']
full_name = ""
#traversing the list
for name in name_list:
full_name += name
print(full_name)
Output:
Cristiano_Ronaldo_dos_Santos_Aveiro
We have explained four different ways to convert a list to string along with an introduction to Python lists and String. It is important to study every method and function in detail so you can efficiently use any abovementioned method as per your requirements.
SPEC INDIA, as your single stop IT partner has been successfully implementing a bouquet of diverse solutions and services all over the globe, proving its mettle as an ISO 9001:2015 certified IT solutions organization. With efficient project management practices, international standards to comply, flexible engagement models and superior infrastructure, SPEC INDIA is a customer’s delight. Our skilled technical resources are apt at putting thoughts in a perspective by offering value-added reads for all.
“SPEC House”, Parth Complex, Near Swastik Cross Roads, Navarangpura, Ahmedabad 380009, INDIA.
“SPEC Partner”, 350 Grove Street, Bridgewater, NJ 08807, United States.
This website uses cookies to ensure you get the best experience on our website. Learn more