[Python-Dev] Format strings, Unicode, and Py2.7: need clarification

Craig Rodrigues rodrigc at crodrigues.org
Wed May 17 17:41:29 EDT 2017


Hi,

While cleaning up some code during Python 2 -> Python 3 porting,
I switched some code to use str.format(), I found this behavor:

Python 2.7
=========
a = "%s" % "hi"
b = "%s" % u"hi"
c = u"%s" % "hi"
d = "{}".format("hi")
e = "{}".format(u"hi")
f = u"{}".format("hi")

type(a) == str
type(b) == unicode
type(c) == unicode
type(d) == str
type(e) == str
type(f) == unicode

My intuition would lead me to believe that type(b)
and type(e) would be the same (unicode), but they are not.
The confusion for me is why is type(e) of type str, and not unicode?

Can someone clarify this for me?

I understand that in Python 3, all these cases are str, so it is not
as big a problem there, but I am trying to keep things working on
Python 2.7.

Thanks.
--
Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170517/383b0176/attachment.html>


More information about the Python-Dev mailing list