Issue 9139: Add examples for str.format() (original) (raw)

Created on 2010-07-02 02:43 by ezio.melotti, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (11)

msg109087 - (view)

Author: Ezio Melotti (ezio.melotti) * (Python committer)

Date: 2010-07-02 02:43

The attached patch adds a section with examples about str.format() after http://docs.python.org/library/string.html#format-specification-mini-language . The patch needs some small improvements:

  1. the examples in the previous sections could be removed and/or a link to the new section could be added;
  2. the last example could be improved;
  3. another example using custom formats (e.g. for datetime) should be added;

I'd like to have this in 2.7 (Benjamin said it's OK), please review.

msg109096 - (view)

Author: Éric Araujo (eric.araujo) * (Python committer)

Date: 2010-07-02 10:34

The examples seem good to me after a quick reading. There’s not too much but still enough.

I find the phrasing of the versionchanged not easily understandable: “The position of the arguments can now be omitted.” Clearer and probably too verbose: “When using numeric placeholders that follow the order of the positional arguments, you can omit the numbers: '{} {!s}' is equivalent to '{0} {1!s}'”

msg109115 - (view)

Author: Eric V. Smith (eric.smith) * (Python committer)

Date: 2010-07-02 14:23

Thanks for taking the lead on this.

In this sentence: +The new format syntax also supports new and different options, showed in the +follow examples. "showed" should be "shown".

I like to use an example showing attribute access on named types. Barry Warsaw suggested using "self", which is a great tip. It's hard to come up with a complete example, but something that ends up like: "x is {self.x}, y is {self.y}".format(self=self)

I think it would be a good idea to show comma formatting (even with its limitations), since there's not another easy way to do that in Python:

'{:,}'.format(1234567890) '1,234,567,890'

For datetime, something like:

import datetime d = datetime.datetime(2010, 7, 4, 12, 15, 58) '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58'

msg109116 - (view)

Author: Eric V. Smith (eric.smith) * (Python committer)

Date: 2010-07-02 14:24

Instead of "named types", I meant "named arguments", of course.

msg109120 - (view)

Author: Ezio Melotti (ezio.melotti) * (Python committer)

Date: 2010-07-02 18:28

Here is a new patch. I fixed the typos, removed the examples from the first section and added a link to the examples section. I also added the examples suggested by Eric plus an example about {:%}, and followed the suggestion of Éric and clarified the versionchanged message.

msg109132 - (view)

Author: Eric V. Smith (eric.smith) * (Python committer)

Date: 2010-07-02 20:47

This looks like a definite improvement to me.

When you merge to py3k, don't forget to change the versionchanged tag to 3.1.

msg109137 - (view)

Author: Éric Araujo (eric.araujo) * (Python committer)

Date: 2010-07-02 21:33

I’m not sure it’s good style to say “now” twice (in the versionchanged block).

I almost feel bad pointing that there is one line with bad whitespace:

Otherwise great patch :)

msg109138 - (view)

Author: Eric V. Smith (eric.smith) * (Python committer)

Date: 2010-07-02 21:41

I think I'd just delete both instances of "now". And now that I read it more critically, "position" should be "positional". How's this rewording:

.. versionchanged:: 2.7 The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.

msg109139 - (view)

Author: Ezio Melotti (ezio.melotti) * (Python committer)

Date: 2010-07-02 21:50

Here is another patch. I also added another example that uses the fillchar.

msg109145 - (view)

Author: Eric V. Smith (eric.smith) * (Python committer)

Date: 2010-07-02 22:23

Looks good.

msg109150 - (view)

Author: Ezio Melotti (ezio.melotti) * (Python committer)

Date: 2010-07-02 23:24

Committed in r82457 and r82459 (trunk), r82460 (release26-maint), r82462 (py3k), r82463 (release31-maint). Thanks for the reviews!

History

Date

User

Action

Args

2022-04-11 14:57:03

admin

set

github: 53385

2010-07-02 23:24:31

ezio.melotti

set

status: open -> closed
resolution: accepted -> fixed
messages: +

stage: patch review -> resolved

2010-07-02 22:23:48

eric.smith

set

messages: +

2010-07-02 21:50:59

ezio.melotti

set

files: + issue9139v3.diff

messages: +

2010-07-02 21:41:17

eric.smith

set

messages: +

2010-07-02 21:33:23

eric.araujo

set

resolution: accepted
messages: +

2010-07-02 20:47:06

eric.smith

set

messages: +

2010-07-02 18:32:07

mark.dickinson

set

nosy: + mark.dickinson

2010-07-02 18:28:37

ezio.melotti

set

files: + issue9139v2.diff

messages: +

2010-07-02 14:24:47

eric.smith

set

messages: +

2010-07-02 14:23:41

eric.smith

set

messages: +

2010-07-02 10:34:23

eric.araujo

set

nosy: + eric.araujo
messages: +

2010-07-02 02:43:07

ezio.melotti

create