Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created May 20, 2014 19:33
Show Gist options
  • Save slinkp/5258f2107ff0ccc5bd0d to your computer and use it in GitHub Desktop.
Save slinkp/5258f2107ff0ccc5bd0d to your computer and use it in GitHub Desktop.
Fun with the "%s" string formatter in python
# It accepts a precision??
In [13]: print '%.5s' % "Oh hi this is a long string"
Oh hi
# It magically knows whether you need a unicode or bytestring representation.
In [10]: class Foo2(object):
....: def __str__(self): return "stringy"
....: def __unicode__(self): return u"unicodey"
....:
In [11]: "%s" % Foo2()
Out[11]: 'stringy'
In [12]: u"%s" % Foo2()
Out[12]: u'unicodey'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment