TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Convert a List to String in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Convert a List to String in Python

Last updated: Nov 05, 2023 12:08 am
By Meenakshi Agarwal
Share
5 Min Read
Convert Python list to string with examples
Convert Python list to string with examples
SHARE

In this tutorial, you can quickly discover the most efficient methods to convert Python List to String. Several examples are provided to help for clear understanding.

Contents
Python Join() SyntaxConvert Python List of Strings to a string using join()Convert a list of chars into a stringConversion of a mixed list to a string using join()Get a comma-separated string from a list of numbers

Python provides a magical join() method that takes a sequence and converts it to a string. The list can contain any of the following object types: Strings, Characters, Numbers.

While programming, you may face many scenarios where list-to-string conversion is required. Let’s now see how can we use the join() method in such different cases.

Check Out: Python String to Int and Int to String Conversion

How to Convert Python List to String

As we’ve told above the Python Join() function can easily convert a list to a string so let’s first check out its syntax.

Python Join() Syntax

The syntax of join() is as follows:

string_token.join( iterable )

Parameters:
iterable => It could be a list of strings, characters, and numbers
string_token => It is also a string such as a space ' ' or comma "," etc.

The above method joins all the elements present in the iterable separated by the string_token. Next, you will see the join() function examples to convert a list to a string.

Convert Python List of Strings to a string using join()

Let’s say we have a list of month names.

# List of month names
listOfmonths = ["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"]

Now, we’ll join all the strings in the above list considering space as a separator.

"""
Desc: 
 Function to convert List of strings to a string with a separator
"""
def converttostr(input_seq, seperator):
   # Join all the strings in list
   final_str = seperator.join(input_seq)
   return final_str

# List of month names
listOfmonths = ["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"]

# List of month names separated with a space
seperator = ' '
print("Scenario#1: ", converttostr(listOfmonths, seperator))

# List of month names separated with a comma
seperator = ', '
print("Scenario#2: ", converttostr(listOfmonths, seperator))

The output is as follows:

Scenario#1:  Jan Feb Mar April May Jun Jul
Scenario#2:  Jan, Feb, Mar, April, May, Jun, Jul

Convert a list of chars into a string

With the help of the join() method, we can also convert a list of characters to a string. See the example given below:

charList = ['p','y','t','h','o','n',' ','p','r','o','g','r','a','m','m','i','n','g']

# Let's convert charList to a string.
finalString = ''.join(charList)

print(charList)
print(finalString)

The output is as follows:

['p', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
python programming

Conversion of a mixed list to a string using join()

Let’s assume that we have got a list of some numbers and strings. Since it is a combination of different objects, we need to handle it a little differently.

sourceList = ["I" , "got", 60, "in", "Science", 70, "in", "English",", and", 66, "in", "Maths"]

It is not possible to use the join() function on this list. We first have to convert each element to a string to form a new one, and then only we can call the join() method.

In the below code, we are converting the list into a string and then adding the elements of two lists.

# Let's convert sourceList to a list of strings and then join its elements.
stringList = ' '.join([str(item) for item in sourceList ])

The final string would appear something like:

I got 60 in Science, 70 in English, and 66 in Maths.

You can check out the full working code below:

sourceList = ["I" , "got", 60, "in", "Science,", 70, "in", "English,", "and", 66, "in", "Maths."]

# Let's convert sourceList to a list of strings and then join its elements.
stringList = ' '.join([str(item) for item in sourceList ])

print(stringList)

Get a comma-separated string from a list of numbers

If you simply wish to convert a comma-separated string, then try the below shortcut:

numList = [20, 213, 4587, 7869]
print(str(numList).strip('[]'))

The output is as follows:

20, 213, 4587, 7869

Alternatively, we can use the map() function to convert the items in the list to a string. After that, we can join them as below:

print (', '.join(map(str, numList)))

The output:

20, 213, 4587, 7869

We can even use the new line character (‘\n’) as a separator. See below:

print ('\n'.join(map(str, numList)))

The result is as follows:

20
213
4587
7869

Must Read – Python Replace String with Examples

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Print alphabet pattern A using range Print Alphabet Pattern “A” Using Range()
Next Article Python join() method with examples Python Join() Method

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x