Python discoveries

Saturday 26 October 2002This is 22 years old. Be careful.

I’ve been using Python for a few years now, but I’m still discovering useful tidbits:

1. The encode method on strings. Want to quickly see how a Unicode string comes out in UTF-8?:

>>> u"\u201c".encode('utf-8')
'\xe2\x80\x9c'

2. The % operator (as in sprintf, not modulo) has a named argument syntax (which seems not to be mentioned in the Python Language Reference, but I discovered it in the Python/Perl phrasebook):

>>> d = {'a': 1, 'b': 2}
>>> "b is %(b)s, a is %(a)s" % d
'b is 2, a is 1'

Comments

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.