[Python-ideas] Desperate need for enhanced print function
Terry Reedy
tjreedy at udel.edu
Sat Sep 5 22:13:28 CEST 2015
On 9/5/2015 3:33 PM, Anand Krishnakumar wrote:
> Hi!
>
> This is my first time I'm sending an email to the python-ideas mailing
> list. I've got an enhancement idea for the built-in print function and I
> hope it is as good as I think it is.
>
> Imagine you have a trial.py file like this:
>
> a = 4
> b = "Anand"
>
> print("Hello, I am " + b + ". My favorite number is " + str(a) + ".")
> OR
> print("Hello, I am ", b, ". My favorite number is ", a, ".")
This prints
Hello, I am Anand . My favorite number is 4 .
because the sep parameter defaults to ' '. If you want 'ease', leave
out end spaces within quotes and don't worry about spaces before periods.
To get what you want, add ", sep=''" before the closing parenthesis.
print("Hello, I am ", b, ". My favorite number is ", a, ".", sep='')
Hello, I am Anand. My favorite number is 4.
> Well I've got an idea for a function named "print_easy" (The only valid
> name I could come up with right now).
When you want a mix of '' and ' ' separators, learn to use templates.
print("Hello, I am {}. My favorite number is {}.".format(b, a))
Hello, I am Anand. My favorite number is 4.
This ends up being easier to type and read because is does not have all
the extraneous unprinted commas and quotes in the middle.
The formatting could also be written
print("Hello, I am {name}. My favorite number is {favnum}."
.format(name=b, favnum=a))
--
Terry Jan Reedy
More information about the Python-ideas
mailing list