Python Forum
help with commas in print functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with commas in print functions
#11
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
Reply
#12
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Commas issue in variable ddahlman 6 463 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Print different positions in loop from functions konsular 5 2,735 Oct-16-2019, 08:10 PM
Last Post: buran
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 2,376 Jul-20-2019, 06:59 AM
Last Post: scidam
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,130 Jul-02-2019, 02:29 PM
Last Post: metulburr
  Automate the boring stuff, inserting commas in list DJ_Qu 3 4,722 Apr-21-2019, 03:52 PM
Last Post: perfringo
  accessing array without commas rjnabil1994 1 2,516 Feb-10-2019, 06:29 PM
Last Post: Larz60+
  Printing array without commas pawlaczkaj 1 9,462 Apr-08-2018, 07:05 PM
Last Post: Larz60+
  How to remove empty line between two print functions BigEasy 1 2,387 Feb-07-2018, 08:38 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020