Python way of printing: with 'format' or percent form?

86,718

Solution 1

Use the format method, especially if you're concerned about Python 3 and the future. From the documentation:

The formatting operations described here are modelled on C's printf() syntax. They only support formatting of certain builtin types. The use of a binary operator means that care may be needed in order to format tuples and dictionaries correctly. As the new :ref:string-formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.

Solution 2

.format was introduced in Python2.6

If you need backward compatibility with earlier Python, you should use %

For Python3 and newer you should use .format for sure

.format is more powerful than %. Porting % to .format is easy but the other way round can be non trivial

Solution 3

The docs say that the format method is preferred for new code. There are currently no plans to remove % formatting, though.

Solution 4

You can use both .No one said % formatting expression is deprecated.However,as stated before the format method call is a tad more powerful. Also note that the % expressions are bit more concise and easier to code.Try them and see what suits you best

Share:
86,718
Alex
Author by

Alex

Updated on July 22, 2022

Comments

  • Alex
    Alex almost 2 years

    In Python there seem to be two different ways of generating formatted output:

    user = "Alex"
    number = 38746
    print("%s asked %d questions on stackoverflow.com" % (user, number))
    print("{0} asked {1} questions on stackoverflow.com".format(user, number))
    

    Is there one way to be preferred over the other? Are they equivalent, what is the difference? What form should be used, especially for Python3?

  • guettli
    guettli over 10 years
    New documentation says this about the percent operator: "The formatting operations described here exhibit a variety of quirks that lead to a number of common errors ..." but it is not deprecated. docs.python.org/3/library/…
  • BrenBarn
    BrenBarn over 10 years
    It's not officially deprecated, but you're encouraged not to use it.
  • guettli
    guettli over 10 years
  • yossi
    yossi over 9 years
    how is it more powerful ?
  • John La Rooy
    John La Rooy over 9 years
    @yossi Here's one example stackoverflow.com/a/3228928/174728. It also lets you do key/index lookups import sys;"{[version]}".format(vars(sys))
  • sshilovsky
    sshilovsky over 8 years
    It's probably worth noting that logging module uses %-like syntax, so it may be preferred for consistency reasons.
  • Pax0r
    Pax0r over 8 years
    how about: import sys;"%(version)s" % (vars(sys))
  • John La Rooy
    John La Rooy over 8 years
    @Pax0r: The linked example is '{num:{fill}{width}}'.format(num=123, fill='0', width=6). You can't do this with % unless you "double escape" and apply it twice
  • Qi Fan
    Qi Fan about 8 years
    Is .format significantly slower than % ?
  • joelostblom
    joelostblom almost 7 years
    From Python 3.6, you can use f-strings to access previously defined variables: print(f"{user} asked {number} questions on stackoverflow.com"
  • Sean Bradley
    Sean Bradley about 5 years
    '%s' % (str(123).rjust(6, '0'))
  • John La Rooy
    John La Rooy about 5 years
    @Sean, that's formatting a string, not a number. If I just manipulate a value into a string someway there is not need for an extra layer of formatting at all. Indeed '%s'% adds nothing extra to the string you already created.
  • Sean Bradley
    Sean Bradley about 5 years