print & string formatting

Chris Angelico rosuav at gmail.com
Sun Jul 22 00:52:50 EDT 2018


On Sun, Jul 22, 2018 at 2:53 PM, Abdur-Rahmaan Janhangeer
<arj.python at gmail.com> wrote:
> *Folks,I get a lot confused while using print functions in Python.For
> example, I get the same results for the following code:str = "one two
> three"print strprint "%s" %(str)So, what is the need to use the second
> method which I see being used in many programs I am referring to*
>
> well
> 1) that is more convenient than concatenation ""+x+""
> 2) no need for casting
> x = 64
> "%d" %(x)
> 3) the .format() or f'' makes it simpler and reflects the real spirit of py
> "{}".format(y) # no need to specify s or d etc

Not sure what you mean by "the real spirit of Python", but you can use
%s with literally any object at all, and it'll work - in fact, it'll
do the same thing that {} does in a format string.

But yes, the point of the formatting operations is to do more than
just format a string.

ChrisA



More information about the Python-list mailing list