Print comma separated list python

Pass sep="," as an argument to print()

You are nearly there with the print statement.

There is no need for a loop, print has a sep parameter as well as end.

>>> print(*range(5), sep=", ")
0, 1, 2, 3, 4

A little explanation

The print builtin takes any number of items as arguments to be printed. Any non-keyword arguments will be printed, separated by sep. The default value for sep is a single space.

>>> print("hello", "world")
hello world

Changing sep has the expected result.

>>> print("hello", "world", sep=" cruel ")
hello cruel world

Each argument is stringified as with str(). Passing an iterable to the print statement will stringify the iterable as one argument.

>>> print(["hello", "world"], sep=" cruel ")
['hello', 'world']

However, if you put the asterisk in front of your iterable this decomposes it into separate arguments and allows for the intended use of sep.

>>> print(*["hello", "world"], sep=" cruel ")
hello cruel world

>>> print(*range(5), sep="---")
0---1---2---3---4

Using join as an alternative

The alternative approach for joining an iterable into a string with a given separator is to use the join method of a separator string.

>>>print(" cruel ".join(["hello", "world"]))
hello cruel world

This is slightly clumsier because it requires non-string elements to be explicitly converted to strings.

>>>print(",".join([str(i) for i in range(5)]))
0,1,2,3,4

Brute force - non-pythonic

The approach you suggest is one where a loop is used to concatenate a string adding commas along the way. Of course this produces the correct result but its much harder work.

>>>iterable = range(5)
>>>result = ""
>>>for item, i in enumerate(iterable):
>>>    result = result + str(item)
>>>    if i > len(iterable) - 1:
>>>        result = result + ","
>>>print(result)
0,1,2,3,4

Strings in Python are a sequence of characters or a collection of bytes. However, Python does not support character data type but it considers a single character a string with a span of 1. We use quotes to generate strings in Python. Also, we use square brackets to access string elements in python. Python strings are absolute. Now, let’s discuss how to create a Python list of comma-separated strings.

The list in Python is indexed in order and starts from 0. The most versatile thing about a list is that every item in the list has its place. If you convert a list into a comma-separated string, then your resultant string contains an element of the list that is separated by a comma. Come, let’s elaborate on it with the help of illustrations. We use the Spyder compiler to describe the working of the python list to a comma-separated string.

Example 1

In this example, we use the join () + str () function to make a python list separated by a comma. This is a very simple and easy process. Now let’s check how the code works in Python. At first, launch Spyder IDE in Windows 10. To launch it simply type ‘Spyder’ in your Windows PC search bar and then click open. Press the key combination ‘Ctrl+Shift+N’ to create a new file. Once done, write a Python code to elaborate on how to make a Python list to a comma-separated string.

At first, we initialize a list and use the print function to print original values in the list. Then we initialize a ‘delim’. After initializing the delimiter, we use the map function to convert every list item into a string. We can then use the join function to add a comma at the end of each value, after altering each value to a string. At last, we use the print function which prints the output on the console screen. The code is shown in textual form. Also, we have attached a screenshot as well.

Original_list = [6, "Hfh", 9, "is", "good", 2]

print("Our list is : " + str(Original _list))

delim = "*"

temp = list(map(str, Original _list))

result = delim.join(temp)

print("List to comma separated string: " + str(result))

Print comma separated list python

After you successfully write the Python code, now it is time to save your code file with the ‘.py’ extension as below. The file name may be changed in your illustration.

Print comma separated list python

Now, run the file or simply use the “F9” shortcut key to check the output of a Python list to a comma-separated string. The output is displayed in the appended image.

Print comma separated list python

Example 2

In our second example, we use the loop + str () function to find a python list to a comma-separated string. Now, let’s check how the program works. Let’s move to the Spyder compiler in Windows 10 and select a new empty file or use the same file “StringList1.py”. We use the same code file “StringList1.py” and made changes to it. We use another way to demonstrate the working of a Python list to a comma-separated string.

At first, we initialize a list and use the print function to print original values in the list. Then we use a loop to add a comma at the end of each value, after altering each value to a string. At last, we use the print function which prints the output on the console screen. The code is shown in textual form. Also, we have attached a screenshot as well.

string_list = [8, "Hfh", 0, "is", "good", 2]

print("Our list is : " + str(string _list))

delim = "*"

result = ''

for ele in string_list:

Result = result + str(ele) + delim

print("List to comma separated string: " + str(result))

Print comma separated list python

Again, save the “StringList1.py” file for further execution. Then again, build and run the code or simply use the F9 key to check the output of a Python list to a comma-separated string. After compiling the above program, you will get the desired output. The output is displayed in the appended image.

Print comma separated list python

Conclusion

In this tutorial, we discussed the importance of Python string and how to make a Python list to comma separated string using the Spyder compiler. We have listed two different and unique ways to implement our article. You can use any other method to implement comma-separated strings in Python language.

About the author

Hello, I am a freelance writer and usually write for Linux and other technology related content

How do you get comma

how to split an input in python by comma.
lst = input(). split(',')#can use any seperator value inside '' of split..
print (lst).
#given input = hello,world..
#output: ['hello', 'world'].
#another example..
lst = input(). ... .
print (lst).
#given input = hello ; world ; hi there..

How do you separate a list with commas?

When making a list, commas are the most common way to separate one list item from the next. The final two items in the list are usually separated by "and" or "or", which should be preceeded by a comma. Amongst editors this final comma in a list is known as the "Oxford Comma".

How do you convert a list to a comma

Approach: This can be achieved with the help of join() method of String as follows. Get the List of String. Form a comma separated String from the List of String using join() method by passing comma ', ' and the list as parameters. Print the String.

How do you print a comma

Use str..
a_list = ["a", "b", "c"].
joined_string = ",". join(a_list) Concatenate elements of `a_list` delimited by `","`.
print(joined_string).