String formatting: % vs. .format vs. string literal

后端 未结 16 2722
青春惊慌失措
青春惊慌失措 2020-11-21 04:18

Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations?

Pyt

16条回答
  •  长情又很酷
    2020-11-21 04:45

    But please be careful, just now I've discovered one issue when trying to replace all % with .format in existing code: '{}'.format(unicode_string) will try to encode unicode_string and will probably fail.

    Just look at this Python interactive session log:

    Python 2.7.2 (default, Aug 27 2012, 19:52:55) 
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
    ; s='й'
    ; u=u'й'
    ; s
    '\xd0\xb9'
    ; u
    u'\u0439'
    

    s is just a string (called 'byte array' in Python3) and u is a Unicode string (called 'string' in Python3):

    ; '%s' % s
    '\xd0\xb9'
    ; '%s' % u
    u'\u0439'
    

    When you give a Unicode object as a parameter to % operator it will produce a Unicode string even if the original string wasn't Unicode:

    ; '{}'.format(s)
    '\xd0\xb9'
    ; '{}'.format(u)
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0439' in position 0: ordinal not in range(256)
    

    but the .format function will raise "UnicodeEncodeError":

    ; u'{}'.format(s)
    u'\xd0\xb9'
    ; u'{}'.format(u)
    u'\u0439'
    

    and it will work with a Unicode argument fine only if the original string was Unicode.

    ; '{}'.format(u'i')
    'i'
    

    or if argument string can be converted to a string (so called 'byte array')

提交回复
热议问题