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
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.
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
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).
Look into fstring
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
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)
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.
@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
(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.
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.
Pages: 1 2