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
#1
I'm trying to separate numbers, with a comma, within a print function. I've tried to find anything on it, but I couldn't find anything. Its probably a simple solution that I'm not finding. But for example:
print("The input numbers are ", First_number, Second_number, Third_number)
with a desired output of:
Output:
The input numbers are x, y, z
but I'm getting an output of
Output:
The input numbers are x y z
I hope this is enough information to go off of.

Thanks ahead of time.
Yoriz write Oct-12-2021, 08:40 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button
Reply
#2
You can use the string method join
user_inputs = ["X", "Y", "Z"]
print(f"The input numbers are {', '.join(user_inputs)}")
Output:
The input numbers are X, Y, Z
Reply
#3
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).
Underscore likes this post
Reply
#4
Look into fstring
ndc85430 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Reading over fstrings, I do not believe that they are the solution to my problem. The closest thing that I am getting to the answer that I seek is just to make a list and insert it into the print line. I am just trying to make it so I don't have the brackets to open and close in the output.

Output:
The input numbers are (X, Y, Z)
except I don't want the brackets
Reply
#6
The full code is
First_number = float(input("Enter first number "))
Second_number = float(input("Enter second number "))
Third_number = float(input("Enter third number "))

Total = First_number + Second_number + Third_number
Average = Total / 3

User_inputs = First_number , Second_number , Third_number

print("The total is ",Total)
print("The average is ",Average)
print(f"The input numbers are", User_inputs)
with and output of
Output:
Enter first number 7 Enter second number 8 Enter third number 9 The total is 24.0 The average is 8.0 The input numbers are (7.0, 8.0, 9.0)
Reply
#7
f-strings are a good idea. The commas are part of the format of the output you want, so put them in the string and use placeholders for the variable parts.
Reply
#8
@kronhamilton, If you don't follow the thread, there is no point for us to answer. @bowlofred already show what to do with f-string, like 3 hours ago

https://python-forum.io/thread-35246-pos...#pid148605
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Oct-12-2021, 11:55 AM)buran Wrote: @kronhamilton, If you don't follow the thread, there is no point for us to answer. @bowlofred already show what to do with f-string, like 3 hours ago

https://python-forum.io/thread-35246-pos...#pid148605

It was all a misunderstanding, I was inputting the code wrong, so I just assumed that it wasn't what I was looking for. I did go back to look at it again and I figured it out. Thanks @bowlofred.
Reply
#10
You can use join, but join expects strings. I get around the issue by not converting the user input to numbers. I need to do a conversion to sum the numbers, but I don't keep the results around after I am done doing that. map(func, iterator) calls func(value) for each value in the iterator. Here I use it to convert my number strings to floats so they can be summed. The conversion will likely raise an exception, so I catch the exception and remind the user to input numbers.
user_inputs = [
    input("Enter first number "),
    input("Enter second number "),
    input("Enter third number ")]

try:
    total = sum(map(float, user_inputs))
except ValueError:
    print("Not all inputs are numbers")
    total = 0
average = total / len(user_inputs)

print("The total is ", total)
print("The average is ",average)
print("The input numbers are", ', '.join(user_inputs))
This can easily be made to work with any number of inputs. Here I use split(separator) to split the user input string into a list of substrings.
user_inputs =input('Enter numbers separated by "," ').split(',')

try:
    total = sum(map(float, user_inputs))
except ValueError:
    print("Not all inputs are numbers")
    total = 0
average = total / len(user_inputs)

print("The total is ", total)
print("The average is ",average)
print("The input numbers are", ', '.join(user_inputs))
Of course if you really want the inputs as numbers you can do that too. For average I need a list of numbers so I can get the len() of the list. map() returns an iterator, and iterators don't have len() because they could potentially iterate forever. I use the list() function to extract all the values from the iterator and put them in a list.
try:
    user_inputs = list(map(float, input('Enter numbers separated by "," ').split(',')))
except ValueError:
    print("Not all inputs are numbers")
    user_inputs = [0]

total = sum(user_inputs)
average = total / len(user_inputs)

print("The total is ", total)
print("The average is ", average)
print("The input numbers are", ', '.join(map(str, user_inputs)))
Since user_inputs are now numbers I need to convert them back to strings for the join function.
Reply


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