Posts: 5
Threads: 2
Joined: Oct 2021
Oct-12-2021, 08:22 AM
(This post was last modified: Oct-12-2021, 08:40 AM by Yoriz.
Edit Reason: Added code tags
)
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:
1 |
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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
You can use the string method join
1 2 |
user_inputs = [ "X" , "Y" , "Z" ]
print ( f "The input numbers are {', '.join(user_inputs)}" )
|
Output: The input numbers are X, Y, Z
Posts: 1,583
Threads: 3
Joined: Mar 2020
Oct-12-2021, 08:47 AM
(This post was last modified: Oct-12-2021, 08:48 AM by bowlofred.)
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.
1 |
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
Posts: 1,145
Threads: 114
Joined: Sep 2019
Posts: 5
Threads: 2
Joined: Oct 2021
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
Posts: 5
Threads: 2
Joined: Oct 2021
The full code is
1 2 3 4 5 6 7 8 9 10 11 12 |
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)
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 8,166
Threads: 160
Joined: Sep 2016
@ 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
Posts: 5
Threads: 2
Joined: Oct 2021
(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.
Posts: 6,809
Threads: 20
Joined: Feb 2020
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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.
|