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

Eric V. Smith eric at trueblade.com
Wed May 17 21:56:59 EDT 2017


> On May 17, 2017, at 2:41 PM, Craig Rodrigues <rodrigc at crodrigues.org> wrote:
> 
> 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 think it's because I wanted to return str if possible, and didn't want to find out that one of the calls to __format__ returned unicode, and then go back and convert all of the previous results to unicode from str.

And, I guess we didn't consider it important enough at the time.

Eric. 

> 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
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com



More information about the Python-Dev mailing list