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 i, item in enumerate(iterable):
>>>    result = result + str(item)
>>>    if i > len(iterable) - 1:
>>>        result = result + ","
>>>print(result)
0,1,2,3,4
Answer from Graeme Stuart on Stack Overflow
Top answer
1 of 8
51

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 i, item in enumerate(iterable):
>>>    result = result + str(item)
>>>    if i > len(iterable) - 1:
>>>        result = result + ","
>>>print(result)
0,1,2,3,4
2 of 8
7

You can use str.join() and create the string you want to print and then print it. Example -

print(','.join([str(x) for x in range(5)]))

Demo -

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

I am using list comprehension above, as that is faster than generator expression , when used with str.join .

🌐
Quora
quora.com › How-do-you-print-comma-separated-values-in-Python
How to print comma separated values in Python - Quora
Answer (1 of 3): If you already have your values in a list, you can use the join() method: print(“, “.join(my_list)) You can also use the print() function, with the sep (i.e., separator) argument: print(a, b, c, d, sep=”, “)
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-35.php
Python: Display a number with a comma separator - w3resource
print() # Print the original value of 'x' with a label. print("Original Number: ", x) # Format the value of 'x' with a comma separator for thousands and print it. print("Formatted Number with comma separator: "+"{:,}".format(x)) # Print the ...
🌐
GeeksforGeeks
geeksforgeeks.org › print-number-commas-1000-separators-python
Print number with commas as 1000 separators in Python - GeeksforGeeks
May 14, 2025 - In this program, we need to print the output of a given integer in international place value format and put commas at the appropriate place, from the right. Let's see an example of how to print numbers with commas as thousands of separators in Python.
🌐
Deepnote
deepnote.com › app › sam-ola-ll › SamOlaModuleTwoLessonThreeActivityTwo-aa8a4436-05e4-483d-bb23-116c6ba643b5
Concept: Comma Print Formatting
November 10, 2023 - #Sam Ola #9/29/2021 #In this lesson i learned how to combine integers with strings #The problems i came across was adding a comma to the beginning of my code for the owner on task 5 and when i removed it, it worked · # review and run code name = "Collette" print("Hello " + name + "!") print("Hello to",name,"who is from the city")
🌐
Alexwlchan
alexwlchan.net › notes › 2025 › python-comma-n
Print a comma-separated number in Python with {num:,} – alexwlchan
You can use `{num:,}` to insert a comma every three digits, `{num:_}` to insert an underscore every three digits, and `{num:n}` to insert a locale-aware digit separator.
🌐
Reddit
reddit.com › r/python › using commas (,) instead of plus (+)
r/Python on Reddit: Using commas (,) instead of plus (+)
September 14, 2016 -

Hi!

I'm coding in Python for the first time, so forgive me if my question is pre-school basic.

Could someone explain what the difference is between using commas and pluses inside print()? I ask this because commas result in automatic spaces, whereas you have to add spaces inside each string when using pluses. See below

print(The product: " + product + " costs " + price + " dollars")
print("The product:", product, "costs", price, "dollars")

To a newbie like me, it seems like using plus signs means a lot more work considering you have to manually input spaces. But I assume there is a syntactic (and perhaps also semantic) difference. Could someone enlighten me as to how these two separate lines of code differ from each other?

Thank you!

EDIT: I know that using plus means I have to change product -> str(product) and price -> str(product), but that just further proves my point... Doesn't it mean a lot more work?

Top answer
1 of 16
2569

Locale-agnostic: use _ as the thousand separator

f'{value:_}'          # For Python ≥3.6

Note that this will NOT format in the user's current locale and will always use _ as the thousand separator, so for example:

1234567 ⟶ 1_234_567

English style: use , as the thousand separator

'{:,}'.format(value)  # For Python ≥2.7
f'{value:,}'          # For Python ≥3.6

Locale-aware

import locale
locale.setlocale(locale.LC_ALL, '')  # Use '' for auto, or force e.g. to 'en_US.UTF-8'

'{:n}'.format(value)  # For Python ≥2.7
f'{value:n}'          # For Python ≥3.6

Reference

Per Format Specification Mini-Language,

The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

and:

The '_' option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type 'd'. For integer presentation types 'b', 'o', 'x', and 'X', underscores will be inserted every 4 digits.

2 of 16
407

I'm surprised that no one has mentioned that you can do this with f-strings in Python 3.6+ as easy as this:

>>> num = 10000000
>>> print(f"{num:,}")
10,000,000

... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_}" uses underscores instead of a comma. Only "," and "_" is possible to use with this method.

This is equivalent of using format(num, ",") for older versions of python 3.

This might look like magic when you see it the first time, but it's not. It's just part of the language, and something that's commonly needed enough to have a shortcut available. To read more about it, have a look at the group subcomponent.

Find elsewhere
🌐
AskPython
askpython.com › home › how to print a number using commas as separators?
How to print a number using commas as separators? - AskPython
March 31, 2023 - Here, the X variable is assigned with value, and after printing the type() function, we get the output as a float type. For more information on data types, please read this article. The f-string is considered a ‘formatted string’ in python. The syntax of the f-string is very easy; it begins with the f letter and ends with the curly braces. The curly braces contain data that we want to replace the string. Let’s see the example of using an f-string as a commas separator.
🌐
Finxter
blog.finxter.com › how-to-print-an-integer-with-commas-as-thousands-separators-in-python
How to Print an Integer with Commas as Thousands Separators in Python? – Be on the Right Side of Change
If you use points as a thousand-separator—for example in 1.000.000 as done in Europe—you can replace the commas in the comma-separated number using the suffix .replace(',', '.') in '{:,}'.format(x).replace(',','.') for any integer number x.
🌐
Deepnote
deepnote.com › app › eeee › Unit-1-Section-1-Activity-7-9b171520-ca5c-48fe-8276-ce4265975035
Unit 1, Section 1, Activity 7
November 10, 2023 - # [ ] use a print() function with comma separation to combine 2 numbers and 2 strings print("If you will get a" , 100,"on your astronomy test, I'll give you" ,20,"dollars, deal?")
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
January 16, 2025 - number = 1234567890 formatted_number = f"{number:,}" print(formatted_number) In this example, the colon (:) followed by a comma (,) inside the curly braces instructs Python to format the number with commas as thousand separators.
🌐
PyTutorial
pytutorial.com › python-print-comma-separated-list
PyTutorial | how to print comma separated list in python
March 8, 2020 - in this article we'll learn how to print comma separated list in python. ','.join(your_list) #list array = ['morocco', 'france', 'italy', 'japan', 'spain'] #print list with comma join = ','.join(array) #print print(join) output · morocco,france,italy,japan,spain ·
🌐
Tutorjoes
tutorjoes.in › Python_example_programs › print_number_commas_thousands_separators_in_python
Write a Python program to print number with commas as thousands separators (from right side)
This program demonstrates the use of string formatting to add commas to large numbers. The format method is used to format each number with commas separating the thousands place, and the resulting formatted string is printed to the console using the print statement.
🌐
TutorialsPoint
tutorialspoint.com › print-number-with-commas-as-1000-separators-in-python
Print number with commas as 1000 separators in Python
Many times the numbers with three or more digits need to be represented suitably using comma. This is a requirement mainly in the accounting industry as well as in the finance domain. In this article we'll see how Python program can be used to insert a comma at a suitable place. We are aiming to insert comma as a thousand separator.
🌐
Python Forum
python-forum.io › thread-29434.html
print scripts example includes comma that seems to serve no purpose
In [1]: print('Enter two integers, and I will tell you', ...: 'the relationships they satisfy.') Enter two integers, and I will tell you the relationships they satisfy. In [2]: print('Enter two integers, and I will tell you' ...: 'the relation...
🌐
w3resource
w3resource.com › python-exercises › math › python-math-exercise-26.php
Python Math: Print number with commas as thousands separators (from right side) - w3resource
Write a Python function that accepts a large integer and returns it as a string with commas separating the thousands. Write a Python script to convert multiple numbers to strings with thousands separators and print them in a column.
🌐
TestMu AI Community
community.testmuai.com › ask a question
Print Number with Commas in Python - Ask a Question - TestMu AI Community
December 25, 2024 - How can I print a number with commas as thousands separators in Python? For example, I want to convert the integer 1234567 into 1,234,567. It does not need to be locale-specific, meaning I just want to use commas as the…
🌐
GeeksforGeeks
geeksforgeeks.org › python-program-to-input-a-comma-separated-string
Input a comma separated string – Python | GeeksforGeeks
May 13, 2025 - # Taking 2 inputs a, b = eval(input("Enter two values\n")) print(a, b) # Taking multiple inputs a = eval(input("Enter multiple values\n")) print(a) ... Explanation: eval() interprets the input string as Python code. Typing 4, 5 returns a tuple (4, 5) automatically. ... While working with Python, we can have problem in which we need to perform the task of splitting the words of string on spaces. But sometimes, we can have comma separated words, which have comma's joined to words and require to split them separately.