Convert a list to string in Python program

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a list iterable, we need to convert it into a string type iterable.

There are four approaches to solve the given problem. Let’s see them one by one−

Brute-force Approach

Example

 Live Demo

def listToString(s):
   # initialize an empty string
   str_ = ""
   # traverse in the string
   for ele in s:
      str_ += ele
   # return string
   return str_
# main
s = ['Tutorials', 'Point']
print(listToString(s))

Output

TutorialsPoint

Using Built-In join() method

Example

def listToString(s):
   # initialize an empty string
   str_ = ""
   # return string
   return (str_.join(s))
# main
s = ['Tutorials', 'Point']
print(listToString(s))

Output

Tutorialspoint

Using List Comprehension

Example

def listToString(s):
   # initialize an empty string
   str_=''.join([str(elem) for elem in s])
   # return string
   return str_
# main
s = ['Tutorials', 'Point']
print(listToString(s))

Output

Tutorialspoint

Using Built-In map() & join() method

Example

 Live Demo

def listToString(s):
   # initialize an empty string
   str_=''.join(map(str, s))
   # return string
   return str_
# main
s = ['Tutorials', 'Point']
print(listToString(s))

Output

Tutorialspoint

Conclusion

In this article, we have learnt about python program to convert a list into a string .
raja
Published on 23-Dec-2019 11:54:33
Advertisements