In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a list we need to convert into a string type.
Here we will be discussing four different approaches to solve the problem statement given above −
Approach 1: Using concatenation in an empty string.
Example
def listToString(s): # empty string str1 = "" # traversal for ele in s: str1 += ele # return string return str1 # main s = ['tutorials’,’point’] print(listToString(s))
Output
tutorialspoint
Approach 2: Using .join() function
Example
def listToString(s): # initialize an empty string str1 = " " # return string return (str1.join(s)) # Driver code s = ['tutorials’,’point’] print(listToString(s))
Output
tutorialspoint
Approach 3: Using list comprehension
Example
s = ['tutorials’,’point’] # using list comprehension listToStr = ' '.join([str(elem) for elem in s]) print(listToStr)
Output
tutorialspoint
Approach 4: Using map() function
Example
s = ['tutorials’,’point’] # using list comprehension listToStr = ' '.join(map(str, s)) print(listToStr)
Output
tutorialspoint
Conclusion
In this article, we learned about the approach to convert list to string.