Created
May 20, 2014 19:33
-
-
Save slinkp/5258f2107ff0ccc5bd0d to your computer and use it in GitHub Desktop.
Fun with the "%s" string formatter in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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