- 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
How to convert list to string in Python?
There may be some situations, where we need to convert a list into a string. We will discuss different methods to do the same.
Iteration
Iterate through the list and append the elements to the string to convert list into a string. We will use for-in loop to iterate through the list elements.
Example
list1=["Welcome","To","Tutorials","Point"] string1="" for i in list1: string1=string1+i string2="" for i in list1: string2=string2+i+" " print(string1) print(string2)
Output
WelcomeToTutorialsPoint Welcome To Tutorials Point
Using .join() method
The list will be passed as a parameter inside the join method.
Example
list1=["Welcome","To","Tutorials","Point"] string1="" print(string1.join(list1)) string2=" " print(string2.join(list1))
Output
WelcomeToTutorialsPoint Welcome To Tutorials Point
Using map()
We can use map() method for mapping str with the list and then use join() to convert list into string.
Example
list1=["Welcome","To","Tutorials","Point"] string1="".join(map(str,list1)) string2=" ".join(map(str,list1)) print(string1) print(string2)
Output
WelcomeToTutorialsPoint Welcome To Tutorials Point
Using list comprehension
Comprehensions in Python provide a short way to construct new sequences using already provided sequences. We will access each element of the list as a string and then use join().
Example
list1=["Welcome","To","Tutorials","Point"] string1="".join(str(elem) for elem in list1) string2=" ".join(str(elem) for elem in list1) print(string1) print(string2)
Output
WelcomeToTutorialsPoint Welcome To Tutorials Point
Advertisements