Python strings are one of the most commonly used data types. Whereas, Python lists are the most commonly used data structures. In this article, we will try to convert a list to a string using different functions in python. We will use string methods like join() and functions like map() and str() to convert a list into string.
List to string using String Concatenation
The easiest way to convert a list to a string in python is by using for loop and string concatenation. To convert a list to a string, we can simply convert each element of the list to a string. Then we can concatenate them to form a string.
To perform the conversion, first we will take an empty string. After that, we will start adding each element of the list to the empty string after converting them to string. In this process, we will also add spaces between each element. This can be done as follows.
myList = ["PFB", 11.2, 11, "Python"]
print("The List is:",myList)
myString = ""
for elem in myList:
myString = myString + str(elem) + " "
print("Output String is:")
print(myString.rstrip())
Output:
The List is: ['PFB', 11.2, 11, 'Python']
Output String is:
PFB 11.2 11 Python
In the above example, we can see that an extra space has been added at the end of the string. We can use the rstrip() method to remove the extra space from the string.
An alternative way to convert a list to string in python is to use the join() method. The join() method is used to create a string from a list of strings.
The join() method is invoked on a separator which is used to separate the elements of the list in the string. The list of strings is given as input to the join() method and It returns a string created by the elements of the list.
We will create an empty string to which all the elements of the list will be concatenated. To create the string, we will take each element of the list one by one and will convert it into string. Then, we will create a list of strings using the string made by previous elements of the list and the current element. We will create the new string from the current string and the previously made string using the join() method by taking a space character as a separator and invoking the join() method on it. We will do this process on all the elements of the list using a for loop till the complete string is formed. This can be seen in the following example.
myList = ["PFB", 11.2, 11, "Python"]
print("The List is:", myList)
myString = ""
for elem in myList:
myString = " ".join([myString, str(elem)])
print("Output String is:")
print(myString.lstrip())
Output:
The List is: ['PFB', 11.2, 11, 'Python']
Output String is:
PFB 11.2 11 Python
In the above method, an extra space is added at the left of the output string which has to be removed using the lstrip() method. To avoid this,Instead of applying str() function on every element of the list, we can use map() function to convert each element of the list into a string so that we can perform the string concatenation using join() method to get the output string using a single statement.
The map() function takes as input a function and an iterable as argument and executes the function on each element of the iterable object and returns the output map object which can be converted into a list.
To convert the elements of the list to strings, we will pass the str() function and the input list to the map() method. After that, we can create a string from a list of strings using the join() method as follows.
myList = ["PFB", 11.2, 11, "Python"]
print("The List is:", myList)
strList = list(map(str, myList))
myString = " ".join(strList)
print("Output String is:")
print(myString.lstrip())
Output:
The List is: ['PFB', 11.2, 11, 'Python']
Output String is:
PFB 11.2 11 Python
Using List Comprehension to convert a list to a string
We can use list comprehension to convert a list into a string. For this, We will convert each element of the list to string using list comprehension and str() function and then we can concatenate them using the join() method as follows.
myList = ["PFB", 11.2, 11, "Python"]
print("The List is:", myList)
strList = [str(i) for i in myList]
myString = " ".join(strList)
print("Output String is:", myString)
Output:
The List is: ['PFB', 11.2, 11, 'Python']
Output String is: PFB 11.2 11 Python
Alternatively, we can use the map() function to convert the elements of the list to string. Then we will create a new list from the map object created from the map() function using list comprehension and will give it as an input to the join() method to create a string from the list as follows.
myList = ["PFB", 11.2, 11, "Python"]
print("The List is:", myList)
myString = " ".join([i for i in map(str, myList)])
print("Output String is:", myString)
Output:
The List is: ['PFB', 11.2, 11, 'Python']
Output String is: PFB 11.2 11 Python
Conclusion
In this article, we have used the str(), map() and join() functions to convert a list to string. We have also seen how to use list comprehension for this task. To know how to convert a string into a list, read this article on string split operation in python.
The post Convert a List to String in Python appeared first on PythonForBeginners.com.
from Planet Python
via read more
No comments:
Post a Comment