.format vs. %

Discussion in 'Python' started by davidfx, Dec 31, 2011.

  1. davidfx

    davidfx Guest

    Hello everyone,
    I just have a quick question about .format and %r %s %d.

    Should we always be using .format() for formatting strings or %?

    Example a = 'apples'
    print "I love {0}.".format(a)

    If I wanted to put .format into a variable, how would I do that.

    Thanks for your information in advance.

    David
     
    davidfx, Dec 31, 2011
    #1
    1. Advertising

  2. What exactly do you mean by putting .format into a variable? You mean like
    this:

    "{name} is very {adj}
    {gender}".format(name="sandy",adj="diligent",gender="female")

    Sat, 31 Dec 2011 20:19:34 +0200 tarihinde davidfx <>
    şöyle yazmış:

    > Hello everyone,
    > I just have a quick question about .format and %r %s %d.
    >
    > Should we always be using .format() for formatting strings or %?
    >
    > Example a = 'apples'
    > print "I love {0}.".format(a)
    >
    > If I wanted to put .format into a variable, how would I do that.
    >
    > Thanks for your information in advance.
    >
    > David



    --
    Opera'nın e-posta istemcisi ile gönderildi: http://www.opera.com/mail
     
    Yaşar Arabacı, Dec 31, 2011
    #2
    1. Advertising

  3. davidfx

    Andrew Berg Guest

    On 12/31/2011 12:19 PM, davidfx wrote:
    > Should we always be using .format() for formatting strings or %?

    In new code, yes. %-style formatting will eventually go away, but
    probably not for a long time.

    > If I wanted to put .format into a variable, how would I do that.

    What do you mean?

    --
    CPython 3.2.2 | Windows NT 6.1.7601.17640
     
    Andrew Berg, Dec 31, 2011
    #3
  4. davidfx

    davidfx Guest

    Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.

    formatter = "%r %r %r %r"

    print formatter % (1, 2, 3, 4)

    What is the .format version of this concept?
     
    davidfx, Dec 31, 2011
    #4
  5. davidfx

    davidfx Guest

    Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.

    formatter = "%r %r %r %r"

    print formatter % (1, 2, 3, 4)

    What is the .format version of this concept?
     
    davidfx, Dec 31, 2011
    #5
  6. How 'bout just:

    >>> s = "{0} {1} {2} {3}"
    >>> s.format(1, 2, 3, 4)

    '1 2 3 4'

    Evan


    On 12/31/2011 13:44, davidfx wrote:
    > Thanks for your response. I know the following code is not going to becorrect but I want to show you what I was thinking.
    >
    > formatter = "%r %r %r %r"
    >
    > print formatter % (1, 2, 3, 4)
    >
    > What is the .format version of this concept?
    >




    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJO/1iuAAoJEAOzoR8eZTzg4jIH/3pDVFzuwDrMG7A0vojG1B8H
    oGxhRb/5lGlpGxIQIU68+isGiO+WFnkCRBhIJYtemEuMyGHqcSBYTH5MwNZ4Gfl3
    Spy4MmOamIR4YzXj3/cR/8LNft0e8ZK4aOiOlmpFcGdSV/PDjPhaBlkzEhm+aHQC
    y6ZMTXd7BiH18ERX2bkZGPF6O+9Sij5aVKyKSkEbH6eV89E/Sy06jEVI0lUeK8xj
    +iTJBT4PCHhcIfhQEGLM7hzKQVqrI1z8IlJaMFrzolMRKy6hRtbEz80UwStMTPwc
    /dqfG10p5ovvWjk1oaT1UMRtgymn8N1jC4gPecoq/kPuovqkZZnRDoE2CqieUes=
    =0YDi
    -----END PGP SIGNATURE-----
     
    Evan Driscoll, Dec 31, 2011
    #6
  7. You mean like this?
    ===========================
    >>> a = "I like {name}"
    >>> a.format(name="myself")

    'I like myself'
    ============================

    Sat, 31 Dec 2011 20:44:08 +0200 tarihinde davidfx <>
    şöyle yazmış:

    > Thanks for your response. I know the following code is not going to be
    > correct but I want to show you what I was thinking.
    >
    > formatter = "%r %r %r %r"
    >
    > print formatter % (1, 2, 3, 4)
    >
    > What is the .format version of this concept?
    >



    --
    Opera'nın e-posta istemcisi ile gönderildi: http://www.opera.com/mail
     
    Yaşar Arabacı, Dec 31, 2011
    #7
  8. On 31.12.2011 19:44, davidfx wrote:
    > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.
    >
    > formatter = "%r %r %r %r"
    >
    > print formatter % (1, 2, 3, 4)
    >
    > What is the .format version of this concept?
    >


    formatter = "{0} {1} {2} {3}"
    formatter.format(1,2,3,4)


    I have to say, I simply hate .format(), and I will be very, very
    upset if classic formatting will really be removed.

    I understand that .format() has a lot of advantages for more complex
    formatting, but for the simple stuff I usually do, it's just
    terribly inconvenient (try {} on a German keyboard).
     
    Alexander Kapps, Dec 31, 2011
    #8
  9. davidfx

    Tim Chase Guest

    On 12/31/11 12:57, Benjamin Kaplan wrote:
    > format is a method of the string class. You store the string the same way
    > you would any other.
    >
    > formatter = "Hello, {}"
    > print(formatter.format("world"))


    Just to note that this syntax doesn't quite work in some earlier
    versions (tested below in 2.6, which came stock in Debian Stable)

    >>> formatter = "Hello, {}"
    >>> print(formatter.format("world"))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: zero length field name in format


    It needs to be spelled "{0}"

    -tkc
     
    Tim Chase, Dec 31, 2011
    #9
  10. davidfx

    Lie Ryan Guest

    On 01/01/2012 05:44 AM, davidfx wrote:
    > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking.
    >
    > formatter = "%r %r %r %r"
    >
    > print formatter % (1, 2, 3, 4)
    >
    > What is the .format version of this concept?
    >


    I don't think the (%r)epr-formatting exist anymore, so if you want to do
    that you'll need to call repr manually.
     
    Lie Ryan, Dec 31, 2011
    #10
  11. davidfx

    Robert Kern Guest

    On 12/31/11 7:34 PM, Lie Ryan wrote:
    > On 01/01/2012 05:44 AM, davidfx wrote:
    >> Thanks for your response. I know the following code is not going to be correct
    >> but I want to show you what I was thinking.
    >>
    >> formatter = "%r %r %r %r"
    >>
    >> print formatter % (1, 2, 3, 4)
    >>
    >> What is the .format version of this concept?
    >>

    >
    > I don't think the (%r)epr-formatting exist anymore, so if you want to do that
    > you'll need to call repr manually.


    Yes, it does.

    formatter = '{!r} {!r} {!r} {!r}'
    print formatter.format(1,2,3,4)

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Dec 31, 2011
    #11
  12. davidfx

    Terry Reedy Guest

    On 12/31/2011 2:24 PM, Tim Chase wrote:
    > On 12/31/11 12:57, Benjamin Kaplan wrote:
    >> format is a method of the string class. You store the string the same way
    >> you would any other.
    >>
    >> formatter = "Hello, {}"
    >> print(formatter.format("world"))

    >
    > Just to note that this syntax doesn't quite work in some earlier
    > versions (tested below in 2.6, which came stock in Debian Stable)


    Autonumbering of {} was introduced, at my suggestion, in 3.1 and
    included with 2.7 (I presume).
    >
    > >>> formatter = "Hello, {}"
    > >>> print(formatter.format("world"))

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > ValueError: zero length field name in format
    >
    >
    > It needs to be spelled "{0}"
    >
    > -tkc
    >
    >



    --
    Terry Jan Reedy
     
    Terry Reedy, Dec 31, 2011
    #12
  13. davidfx

    Miki Tebeka Guest

    > >>> s = "{0} {1} {2} {3}"
    > >>> s.format(1, 2, 3, 4)

    > '1 2 3 4'

    Or even
    In [4]: fmt = '{0} {1} {2} {3}'.format
    In [5]: print(fmt(1, 2, 3, 4))
    1 2 3 4
     
    Miki Tebeka, Jan 1, 2012
    #13
  14. davidfx

    Miki Tebeka Guest

    > >>> s = "{0} {1} {2} {3}"
    > >>> s.format(1, 2, 3, 4)

    > '1 2 3 4'

    Or even
    In [4]: fmt = '{0} {1} {2} {3}'.format
    In [5]: print(fmt(1, 2, 3, 4))
    1 2 3 4
     
    Miki Tebeka, Jan 1, 2012
    #14
  15. davidfx

    Terry Reedy Guest

    On 1/1/2012 4:11 PM, Miki Tebeka wrote:
    >>>>> s = "{0} {1} {2} {3}"
    >>>>> s.format(1, 2, 3, 4)

    >> '1 2 3 4'

    > Or even
    > In [4]: fmt = '{0} {1} {2} {3}'.format
    > In [5]: print(fmt(1, 2, 3, 4))
    > 1 2 3 4


    I have done this, except for using a more informative name, like 'emsg'
    for error message.
    except XError as e:
    print(emsg(a,b,c,e))
    makes for pretty clear code.

    --
    Terry Jan Reedy
     
    Terry Reedy, Jan 2, 2012
    #15
  16. davidfx

    Ethan Furman Guest

    Andrew Berg wrote:
    > On 12/31/2011 12:19 PM, davidfx wrote:
    >> Should we always be using .format() for formatting strings or %?
    >>

    > %-style formatting will eventually go away, but
    > probably not for a long time.


    %-style formatting isn't going away.

    ~Ethan~
     
    Ethan Furman, Jan 2, 2012
    #16
  17. davidfx

    Rick Johnson Guest

    On Dec 31 2011, 12:19 pm, davidfx <> wrote:
    > Hello everyone,
    > I just have a quick question about .format and %r %s %d.
    >
    > Should we always be using .format() for formatting strings or %?


    ALWAYS use the format method over the old and dumpy string
    interpolation. Why? Well because the format method is so much more
    powerful, elegant, and the way of the future. Some might argue that
    format is slower than interpolation (and i can't comment in favor of
    either) HOWEVER, if speed is your concern than interpolation is NOT
    the answer.

    > If I wanted to put .format into a variable, how would I do that.


    That question has been answered many times in this thread already.

    You may find the format spec to be cryptic at first. Well, most find
    regexes cryptic also -- but would anyone recommend NOT using regexes
    just because of crypti-ness? I think not. It's a non-starter.
     
    Rick Johnson, Jan 3, 2012
    #17
  18. davidfx

    Rick Johnson Guest

    On Jan 2, 4:00 pm, Ethan Furman <> wrote:
    > %-style formatting isn't going away.


    You may want to freshen up on the definition of "deprecation". If it
    was NOT going away, why the need to deprecate it? hmm? It would be
    more beneficial if you DO NOT encourage continued usage of this "end-
    of-life" feature.

    http://en.wikipedia.org/wiki/Deprecation
     
    Rick Johnson, Jan 3, 2012
    #18
  19. On Mon, 02 Jan 2012 17:51:48 -0800, Rick Johnson wrote:

    > You may find the format spec to be cryptic at first. Well, most find
    > regexes cryptic also -- but would anyone recommend NOT using regexes
    > just because of crypti-ness? I think not. It's a non-starter.


    I would.

    If you have a task that doesn't *need* a regular expression solution,
    don't use a regular expression.


    For what it's worth, I like the syntax of % formatting. It's nice and
    simple and compact while still being readable. format() is more powerful,
    but when I don't need that power, I stick to % formatting.


    --
    Steven
     
    Steven D'Aprano, Jan 3, 2012
    #19
  20. On Mon, 02 Jan 2012 17:59:43 -0800, Rick Johnson wrote:

    > On Jan 2, 4:00 pm, Ethan Furman <> wrote:
    >> %-style formatting isn't going away.

    >
    > You may want to freshen up on the definition of "deprecation".


    I'm sure Ethan knows the definition of deprecation. I'm sure he also
    knows that % formatting is NOT deprecated. Please stop spreading FUD
    about Python features.



    --
    Steven
     
    Steven D'Aprano, Jan 3, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,205
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,326
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,647
  4. shannon
    Replies:
    0
    Views:
    590
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    480
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page