- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a list to string in Python program
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
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
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 .
Advertisements