Create comma-separated String from List of Strings in Python

In this Python tutorial, we will learn how to make a comma-separated string from a list of strings.

Table Of Contents

Make a comma-separated string from a list of strings using join()

The join() method of the string class in Python, can be used to merge the lists of a string based on a delimeter.

syntax:

",".join(list1)

Here, a comma has been used as a delimeter and list1 is the list of strings. The join() function returns a merged string, containing all the strings from the list separated by the delimeter.

Advertisements

Example 1:

Here we will use the list – [“Welcome”,”to”,”thisPointer”]. It has three strings. We will make a comma-separated string from this list.

list1=["Welcome","to","thisPointer"]

# Actual List
print(list1)

# Get the comma separated string
strValue = ",".join(list1)

print(strValue)

Output:

['Welcome', 'to', 'thisPointer']
Welcome,to,thisPointer

A comma separated string has been created from the list of 3 strings.

Example 2:

Here we will use the list – [“Hello”,”Python”,”thisPointer”,”welcomes”,”you”]. It has 5 strings and we will create a comma-separated string from these strings.

list1=["Hello","Python","thisPointer","welcomes","you"]

# Actual List
print(list1)

# Get the comma separated string
strValue = ",".join(list1)

print(strValue)

Output:

['Hello', 'Python', 'thisPointer', 'welcomes', 'you']
Hello,Python,thisPointer,welcomes,you

A comma separated string is created from the list of 5 strings.

Make a comma-separated string from a list of strings using join() with List Comprehension

The join() function of string class can be used to join the strings in a sequence with a delimeter. We can iterate over all strings in the list and select all or few strings from the list using the list comprehension and then use join() function on it.

syntax:

",".join([iterator for iterator in list1])

Select all of few strings from the list using list comprehension and then pass them to the join() function with ‘,’ as delimeter. It will return a string containing all the strings from list, seperated by comma.

This method is usefull if you want to join few strings from the list based on condition. Let’s understand this with some examples.

Example 1:

Here we will use a list – [“Welcome”,”to”,”thisPointer”], that has 3 strings and we will create a comma-separated string from these strings.

list1=["Welcome","to","thisPointer"]

# Actual List
print(list1)

# Get the comma separated string
strValue = ",".join([i for i in list1])

print(strValue)

Output:

['Welcome', 'to', 'thisPointer']
Welcome,to,thisPointer

A comma separated string is created by joining the strings in the list.

Example 2:

Here we will use a list – [“Hello”,”Python”,”thisPointer”,”welcomes”,”you”] that has five strings. Now we will join only those strings from this list, which has more than 5 characters.

list1=["Hello","Python","thisPointer","welcomes","you"]

# Actual List
print(list1)

# Get the comma separated string
strValue = ",".join([elem for elem in list1 if len(elem) > 5])

print(strValue)

Output:

['Hello', 'Python', 'thisPointer', 'welcomes', 'you']
Python,thisPointer,welcomes

Here, we created a comma-separated string from only those strings from the list which has more than 5 characters.

Make a comma-separated string from a list of strings using join() with map()

Suppose we have a list – [12, “is” , 10, “number”], that has some strings and an integers. We will get a comma-separated string from this list. For that first convert the type all the elements in list as string, then join them using the join() method, like in all previous solutions.

For making all list elements as strings, we can use the map() function. Pass str() function as first parameter and list as second parameter in the map() function. It will apply the str() function on each element of list, to convert them to string and returns a mapped object containing the sequence of converted string elements. Then join all those string elements using join() function and create a merged comma seperated string. For example,

list1=[12, "is" , 10, "number"]

# Actual List
print(list1)

# Get the comma separated string
strValue = ",".join(map(str,list1))

print(strValue)

Output:

[12, 'is', 10, 'number']
12,is,10,number

A comma seperated string has been created from a list.

Summary

We have seen three methods to get a comma-separated string from the list that hold strings or some other types of elements. In all the methods, join() function is used to join the strings. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top