Why do this?

half.italian at gmail.com half.italian at gmail.com
Fri Oct 6 05:43:33 EDT 2006


Nobody's mentioned the ability to save a formatted string and then
substitute the variables later...

string = "There are %s ways to skin a %s"

print string % (3, "furry animal")
print string % (166, "beast")

~half.italian

Matthew Warren wrote:
> Ok, not really python focused, but it feels like the people here could
> explain it for me :)
>
> Now, I started programming when I was 8 with BBC Basic.
>
> I never took any formal classes however, and I have never become an
> expert programmer. I'm an average/hobbyist programmer with quite a few
> languages under my belt but I cant do any really fancy tricks with any
> of them. (although Python might be nudging me into more advanced things,
> now I'm starting to get what all the __method__ thingies and operators
> are all about)
>
> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
>
> Print "There are "+number+" ways to skin a "+furryanimal
>
> But nowadays, I see things like this all over the place;
>
> print("There are %s ways to skin a %s" % (number, furryanimal))
>
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.
>
> I hope I'm not being dense.
>
> The result is that I have pathalogically avoided print "%s" % (thing)
> because it seems to just over complicate things.
>
>
> Ta, :)
>
> Matt.
>
>
>
>
>
> This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.
>
> You should not copy the email, use it for any purpose or disclose its contents to any other person.
> Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
> It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
>
> UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
> Reception Tel: + 44 (0) 115 977 1177
> Support Centre: 0845 607 7070
> Fax: + 44 (0) 115 977 7000
> http://www.digica.com
>
> SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
> Tel: + 27 (0) 21 957 4900
> Fax: + 27 (0) 21 948 3135
> http://www.digica.com




More information about the Python-list mailing list