Cheat sheet for the new string formatting?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Skip Montanaro
I have so far ignored the new string formatting (you know, the stuff with
all the braces, dots and brackets that make Python strings look like Perl
code <wink>). I am still only using Python 2.7, but have recently started
forcing myself to use the print() function. I figure maybe I should also
start to come to grips with the fancy new string formatting.

Is there a cheat sheet around which shows some side-by-side examples of the
{}-style and printf-style? I didn't see anything with a few Google searches.

Thx,

Skip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/39df17a5/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Steven K Knight
June 8 2015 3:11 PM, "Skip Montanaro"  wrote:

        I have so far ignored the new string formatting (you know, the stuff with all the braces, dots and brackets that make Python strings look like Perl code ). I am still only using Python 2.7, but have recently started forcing myself to use the print() function. I figure maybe I should also start to come to grips with the fancy new string formatting.

Is there a cheat sheet around which shows some side-by-side examples of the {}-style and printf-style? I didn't see anything with a few Google searches.

Thx,

Skip

Hi,

I think?http://pyformat.info/ (http://pyformat.info/)?is what you're looking for.

Thanks,
?
--
Steven Knight
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/9deadf36/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Skip Montanaro
On Mon, Jun 8, 2015 at 3:05 PM, Steven K Knight <steven at knight.cx> wrote:

> I think http://pyformat.info/ is what you're looking for.


Perfect, thanks!

S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/56402357/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Skip Montanaro
In reply to this post by Skip Montanaro
This is counterintuitive:

>>> "{:.3}".format(-0.00666762259822)
'-0.00667'
>>> "{:.3f}".format(-0.00666762259822)
'-0.007'
>>> "%.3f" % -0.00666762259822
'-0.007'
>>> "{:.3s}".format(-0.00666762259822)
ValueError Unknown format code 's' for object of type 'float'

Why does the first form display five digits after the decimal point? Why
don't floats support "{:.Ns}"? (I know I can use "{!s}".)

This is using a freshly hg pulled and updated 2.7  branch.

Thx,

S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/d19df931/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Yann Kaiser
It just means significant digits in the general format, which alternates
between 10-exponent notation and plain decimal notation.

https://docs.python.org/3.4/library/string.html#format-specification-mini-language

    >>> '{:.3}'.format(0.0000356785)
    '3.57e-05'
    >>> '{:.3}'.format(0.00356785)
    '0.00357'

On Mon, 8 Jun 2015 at 22:33 Skip Montanaro <skip.montanaro at gmail.com> wrote:

> This is counterintuitive:
>
> >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
> >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
> >>> "%.3f" % -0.00666762259822
> '-0.007'
> >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
>
> Why does the first form display five digits after the decimal point? Why
> don't floats support "{:.Ns}"? (I know I can use "{!s}".)
>
> This is using a freshly hg pulled and updated 2.7  branch.
>
> Thx,
>
> S
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/b7c1f77c/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Serhiy Storchaka-2
In reply to this post by Skip Montanaro
On 08.06.15 23:32, Skip Montanaro wrote:

> This is counterintuitive:
>
>  >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
>  >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
>  >>> "%.3f" % -0.00666762259822
> '-0.007'
>  >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
>
> Why does the first form display five digits after the decimal point? Why
> don't floats support "{:.Ns}"? (I know I can use "{!s}".)

"{:.3}" for floats is equivalent to "{:.3g}".

 >>> "{:.3g}".format(-0.00666762259822)
'-0.00667'
 >>> "%.3g" % -0.00666762259822
'-0.00667'

Format code 's' would produce incorrect and meaningless output for floats.

 >>> "{!s:.3}".format(-0.00666762259822)
'-0.'
 >>> "%.3s" % -0.00666762259822
'-0.'
 >>> "{!s:.3}".format(-0.0000666762259822)
'-6.'
 >>> "%.3s" % -0.0000666762259822
'-6.'



Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

random832@fastmail.us
In reply to this post by Skip Montanaro
On Mon, Jun 8, 2015, at 16:32, Skip Montanaro wrote:

> This is counterintuitive:
>
> >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
> >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
> >>> "%.3f" % -0.00666762259822
> '-0.007'
> >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
>
> Why does the first form display five digits after the decimal point?

Because it's three significant figures. Using .3 alone with a float is
equivalent to %.3g, not %.3f

> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".)

Why would they? The old style didn't support %.Ns either. If you want a
width like "%50s" it's {!s:50}. But if you're using !s (or %s with the
old style) you don't get to specify the significant digits, only the
field width, so it's not clear what old-style code you're trying to find
an equivalent to.

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Mark Lawrence
In reply to this post by Steven K Knight
On 08/06/2015 21:05, Steven K Knight wrote:

>
>
> June 8 2015 3:11 PM, "Skip Montanaro" <skip.montanaro at gmail.com
> <mailto:%22Skip%20Montanaro%22%20<skip.montanaro at gmail.com>>> wrote:
>
>     I have so far ignored the new string formatting (you know, the stuff
>     with all the braces, dots and brackets that make Python strings look
>     like Perl code <wink>). I am still only using Python 2.7, but have
>     recently started forcing myself to use the print() function. I
>     figure maybe I should also start to come to grips with the fancy new
>     string formatting.
>
>     Is there a cheat sheet around which shows some side-by-side examples
>     of the {}-style and printf-style? I didn't see anything with a few
>     Google searches.
>     Thx,
>     Skip
>
>
> Hi,
>
> I think http://pyformat.info/ is what you're looking for.
>
> Thanks,
> ?
> --
> Steven Knight <steven at knight.cx <mailto:steven at knight.cx>>
>

Many thanks for the link, just what the doctor ordered :)

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence


Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Skip Montanaro
In reply to this post by random832@fastmail.us
Skip> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".)

random832> Why would they? The old style didn't support %.Ns either.

Well, the old style does, though it appears the N is ignored:

>>> "%5s" % -0.00666762259822
'-0.00666762259822'

It doesn't raise an exception though.

(This is with Python 2.7.2. Haven't tried to see if that behavior
changed with 3.x.)

One thing which seems obvious now is that since format() delegates to
the individual types for formatting, much of the documentation of this
stuff must now be delegated to the individual types. However, I can't
find anything about the formatting syntax supported by
float.__format__.  Where might I find it documented that "{:.3}" is
synonymous with "%.3g"?

Skip

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

random832@fastmail.us
On Tue, Jun 9, 2015, at 08:15, Skip Montanaro wrote:
> Skip> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".)
>
> random832> Why would they? The old style didn't support %.Ns either.
>
> Well, the old style does, though it appears the N is ignored:
>
> >>> "%5s" % -0.00666762259822
> '-0.00666762259822'

There's no dot there. I was mistaken, though, it does work.

>>> "%.5s" % -0.00666762259822
'-0.00'

The equivalent new-style format is {!s:.5} - the reason is that !s is
fundamentally different from other formats since it performs a type
conversion rather than delegating to the argument's __format__ method.
Whereas _all_ old-style format specifiers did type conversions [see
'%.23f' % Decimal('0.1') vs '{:.23f}'.format(Decimal('0.1'))]

> One thing which seems obvious now is that since format() delegates to
> the individual types for formatting, much of the documentation of this
> stuff must now be delegated to the individual types. However, I can't
> find anything about the formatting syntax supported by
> float.__format__.  Where might I find it documented that "{:.3}" is
> synonymous with "%.3g"?

Where have you looked? Have you read
https://docs.python.org/3/library/string.html#formatspec ?

None    Similar to 'g', except that fixed-point notation, when used, has
at least one digit past the decimal point. The default precision is as
high as needed to represent the particular value. The overall effect is
to match the output of str() as altered by the other format modifiers.

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Skip Montanaro
On Tue, Jun 9, 2015 at 7:29 AM,  <random832 at fastmail.us> wrote:
> Where have you looked? Have you read
> https://docs.python.org/3/library/string.html#formatspec ?

Yes, but I missed the None section. I looked closely at 'g', but
didn't see anything like "this is the default". I will admit I was a
bit frustrated to see things like the documentation for the format()
builtin indicating that it calls value.__format__, but then couldn't
find any documentation in the numeric types for float.__format__. From
the documentation of the format() builtin: "The interpretation of
format_spec will depend on the type of the value argument...", which
suggested to me that at least the builtin types would have some stub
documentation of their __format__ method, even if all it did was
redirect the user to the section in the string doc:
https://docs.python.org/3.5/library/string.html#format-string-syntax

I'll open a doc issue about that. Thanks for the help.

Skip

Reply | Threaded
Open this post in threaded view
|

Cheat sheet for the new string formatting?

Mark Lawrence
In reply to this post by Skip Montanaro
On 09/06/2015 13:15, Skip Montanaro wrote:
>
> One thing which seems obvious now is that since format() delegates to
> the individual types for formatting, much of the documentation of this
> stuff must now be delegated to the individual types. However, I can't
> find anything about the formatting syntax supported by
> float.__format__.  Where might I find it documented that "{:.3}" is
> synonymous with "%.3g"?
>

You won't, at least not that I'm aware of.  I think part of the problem
is that there was a lot of confusion as to whether or not the old style
formatting was to be deprecated.  See http://bugs.python.org/issue14123 
which refers to http://www.gossamer-threads.com/lists/python/dev/969817

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence