sprintf in python (not strictly django, but used all the time in django webapps)

234 views
Skip to first unread message

Cal Leeming [Simplicity Media Ltd]

unread,
Oct 20, 2011, 2:17:20 PM10/20/11
to django...@googlegroups.com
So, just out of curiosity, how many other people didn't realise you
could do this:

>>> print '%(language)s has %(number)03d quote types.' % \
... {"language": "Python", "number": 2}


Instead of this:

print "%s has %03d" % ( "python", "2" )

6 years of python development, and I never found this little beauty. Fail.

Hope this helps someone else.

Cal

Tim Chase

unread,
Oct 20, 2011, 2:21:09 PM10/20/11
to django...@googlegroups.com, Cal Leeming [Simplicity Media Ltd]
On 10/20/11 13:17, Cal Leeming [Simplicity Media Ltd] wrote:
> So, just out of curiosity, how many other people didn't realise you
> could do this:
>
>>>> print '%(language)s has %(number)03d quote types.' % \
> ... {"language": "Python", "number": 2}
>
>
> Instead of this:
>
> print "%s has %03d" % ( "python", "2" )

Also very useful in translations where the order of the pieces
may have to change, allowing the translator to put them where
needed rather than constraining the translator to the order in
which the data is passed.

-tkc

Bill Freeman

unread,
Oct 20, 2011, 2:52:03 PM10/20/11
to django...@googlegroups.com

And fantasticly useful when a given value is needed more than once
in a string, or when you have a collection of available values and a
passed in or chosen format string, such that some values may not
be used at all.

Cal Leeming [Simplicity Media Ltd]

unread,
Oct 20, 2011, 2:54:15 PM10/20/11
to django...@googlegroups.com
Damn right - too many times have my sprintf's ended up looking so ugly
- it was borderline bad code.

Goes to show, no matter how long you've used something, even if it's
day in day out, there's always something new to learn!

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Venkatraman S

unread,
Oct 20, 2011, 11:34:50 PM10/20/11
to django...@googlegroups.com
I use a similar thingy for the ORM queries. Named arguments help in good readability, especially in complex queries.

-V
http://blizzardzblogs.blogspot.com/

Tom Evans

unread,
Oct 24, 2011, 10:09:07 AM10/24/11
to django...@googlegroups.com

Crikey, if you didn't know about that one, you probably don't know
about this one either:

>>> fmt = "{} shalt thou not count, neither count thou {}, excepting that thou then proceed to {}. {} is right out!"
>>> fmt.format('four', 'two', 'three', 'five')
'four shalt thou not count, neither count thou two, excepting that
thou then proceed to three. five is right out!'


http://docs.python.org/library/string.html#format-string-syntax

Cheers

Tom

Cal Leeming [Simplicity Media Ltd]

unread,
Oct 24, 2011, 10:11:44 AM10/24/11
to django...@googlegroups.com
You're correct :L

Thanks for this - I think I might replace my daily break with random Python docs pages, rather than 'theregister' for a few weeks!

Cal

Derek

unread,
Oct 25, 2011, 9:18:05 AM10/25/11
to Django users
Those can be a bit dry... for some variety, you could also try Doug
Hellman's excellent "Module of the Week":
http://www.doughellmann.com/PyMOTW/

On Oct 24, 4:11 pm, "Cal Leeming [Simplicity Media Ltd]"
<cal.leem...@simplicitymedialtd.co.uk> wrote:
> You're correct :L
>
> Thanks for this - I think I might replace my daily break with random Python
> docs pages, rather than 'theregister' for a few weeks!
>
> Cal
>
Reply all
Reply to author
Forward
0 new messages