Python Forum

Full Version: help with commas in print functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Another simple approach is to use sep and end as arguments in your print statement
first = 1
second = 3
third = 2
print("The input numbers are ",end="")
print(first, second, third, sep=", ")
Output:
The input numbers are 1, 3, 2
which also works in pre-f string versions of Python
(Oct-12-2021, 08:47 AM)bowlofred Wrote: [ -> ]Commas separate the arguments to a function. For, print(), each element is printed and the default separator between the elements is a space (but that can be changed).

The important part of your call is that only the first argument is a string. The remaining arguments are variables. You not only want a comma in your output but spaces as well.

F-strings are simple.
print(f"The input numbers are {First_number}, {Second_number}, {Third_number}")
You might prefer to join() the elements together as well if you have several such variables, or if you already have them in a collection (like a list).

Well explained. Voting for this.
Pages: 1 2