syntax difference
Alister
alister.ware at ntlworld.com
Sat Jun 16 15:11:20 EDT 2018
On Sat, 16 Jun 2018 12:01:16 -0700, Sharan Basappa wrote:
> Is there a difference between these prints. The first one looks a bit
> complex. So, why should it be used?
>
> my_age = 35 # not a lie
>
> print "my age %s." % my_age print "my age ", my_age
>
> Output:
> %run "D:/Projects/Initiatives/machine learning/programs/five.py"
> my age 35.
> my age 35
in that example probably no difference - readability counts.
but if you want to display a slightly different message then you could
the following
print "may age is %s years",% my_age
which is arguably nicer than
print "my age is",
print my_age,
print "Years"
& less error prone
you can of course expand this format with multiple insertions & even
explicit formatting for specific data types.
--
It got to the point where I had to get a haircut or both feet firmly
planted in the air.
More information about the Python-list
mailing list