Create Comma-Separated String in Python



In Python, a list is an ordered sequence that can hold several object types, such as integers, characters, or floats. In other programming languages, a list is equivalent to an array. Square brackets [] are used to denote it, and a comma (,) is used to divide two items in the list.

In this article, we will show you how to get a comma-separated string from a list using Python.

Assume we have taken a list containing some random elements. We will return the string separated by commas from the given list.

Comma-Separated String using join()

The join() function is a string function in Python used to join elements of a sequence and then convert the result to a string.

Example

The following program makes a comma-separated string from a list using the join() function -

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python']

# getting the comma-separated string from the list
resultString = ','.join(lst)

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

On executing the above program will generate the following output -

The result string after joining the list with comma separated:
hello,hi,TutorialsPoint,python

Comma-Separated String using join() & map()

The map() function accepts iterable objects like lists, tuples, strings, and so on. Following that, the map() function maps the items of the iterable using the specified function.

map(function, iterable)

Consider the following list,

list = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45]

Which comprises strings, integers, and floating-point numbers. When we use the join() function, we get the following type error.

TypeError: sequence item 4: expected str instance, int found

Example

The following program makes a comma-separated string from a list using the for loop -

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45]

# getting the comma-separated string from the list
resultString = ','.join(map(str, lst))

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

On executing the above program, it will generate the following output -

The result string after joining the list with comma separated:
hello,hi,TutorialsPoint,python,1,45,2.45

We took an example list with some random strings, integers, and floating-point values. Then, using the map() method, we converted each element of the list to a string and used the join() function to get the result.

Comma-Separated String using join() & List comprehension

List comprehension allows for simple Python list generation. It consists of zero or more if statements, a for loop, and an expression enclosed in brackets.

Example

The following program makes a comma-separated string from a list using join() & List comprehension -

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45,True]

# getting the comma-separated string from the list
resultString = ",".join([str(item) for item in lst if item])

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

On executing the above program will generate the following output -

The result string after joining the list with comma separated: 
hello,hi,TutorialsPoint,python,1,45,2.45,True

We took an example list with some random string, integer, floating point, or boolean values. Then, using the list comprehension with for loop to convert each element of the list to a string and using the join() function to get the result.

Comma-Separated String & Removing Empty Values

In this example, we will use the same method but remove empty values from the list.

Example

The following program makes a comma-separated string from a list using join() & removing empty values ?

# input list with None and empty values
lst = ['apple', '', None, 'banana', 'mango', '', 'papaya', None]

# Remove None and empty strings, then join
resultString = ",".join([str(item) for item in lst if item])

# Print the result comma-separated string
print("The result string after removing None and empty values:")
print(resultString)

On running, the above program will generate the following output -

The result string after removing None and empty values:
apple,banana,mango,papaya
Updated on: 2025-04-17T19:21:29+05:30

56K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements