On Sun, Jan 03, 2016 at 02:27:01PM +0200, yehudak . wrote:
> Hi there,
> In a program I wrote the following line (Python 3.5):
>
> print("You've visited", island, '&', new + ".")
>
> A programmer told me that it's a bad habit, and I should have used instead:
>
> print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
>
> May I understand why?
This is nonsense. There is nothing wrong with using print they way you
did.
print is designed to solve simple problems, and you had a simple
problem. format is designed to solve complicated problems. You can use
format, or % string interpolation, to solve simple problems too, but why
bother? These four lines all do more or less the same thing:
print("You've visited", island, '&', new + ".")
print("You've visited {0} {1} {2}{3}".format(island, "&", new, "."))
print("You've visited {} & {}.".format(island, new))
print("You've visited %s & %s." % (island, new))
The second version is the LEAST sensible, it should be re-written as the
third version. But apart from that minor difference, there's no
practical difference between those lines. It is entirely a matter of
personal taste which you use.
This doesn't mean that similar problems will also be a matter of
personal taste. As the problem gets more complex, one or the other gets
more appropriate. But there's nothing wrong with what you wrote.
--
Steve
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor