Python Forum

Full Version: format spec %b is documented but does not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
the format spec %b is documented but does not work:

Output:
lt1/forums /home/forums 1> py2 Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> '%x'%15 'f' >>> '%o'%15 '17' >>> '%b'%15 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: unsupported format character 'b' (0x62) at index 1 >>> lt1/forums /home/forums 2> py3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> '%x'%15 'f' >>> '%o'%15 '17' >>> '%b'%15 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: unsupported format character 'b' (0x62) at index 1 >>> lt1/forums /home/forums 3>
I don't see %b in the documentation (https://docs.python.org/2/library/stdtyp...operations).
it's on page 96 of the PDF edition of The Python Library Reference Release 3.5.2. that's page 108 as the PDF reader in firefox counts pages. see the first row of the second table.
You must use the new format function:
print("int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42))
Output:
int: 42; hex: 2a; oct: 52; bin: 101010
from: https://docs.python.org/3/library/string...t-examples, see the part about "Replacing %x and %o and converting the value to different bases:"
(Jul-26-2017, 04:46 AM)MTVDNA Wrote: [ -> ]You must use the new format function:
print("int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42))
Output:
int: 42; hex: 2a; oct: 52; bin: 101010
from: https://docs.python.org/3/library/string...t-examples, see the part about "Replacing %x and %o and converting the value to different bases:"

are you saying the documentation is wrong?
Looks like some lint. So report it.
(Jul-26-2017, 03:34 AM)Skaperen Wrote: [ -> ]it's on page 96 of the PDF edition of The Python Library Reference Release 3.5.2. that's page 108 as the PDF reader in firefox counts pages. see the first row of the second table.

That document is not available online. I rechecked the 3.5.3 documentation (https://docs.python.org/3.5/library/stdt...formatting), and again, there is no 'b' in the table. Are you sure you are referencing the printf style formatting section and not the format method style formatting?
there are a number of cross references that end up there. i remember my initial reading followed one. maybe i did a bad follow or a bad search.

i guess i should take this opportunity to learn the new format method.
When we taking about new string formatting .format() has been available for a while(9-year).
>>> from datetime import datetime
>>> dt = datetime(2008, 10, 1)
>>> print('.format() was released in Python 2.6 {:{d}}'.format(dt, d='%Y-%m-%d'))
.format() was released in Python 2.6 2008-10-01
3.6 was a big released for Python 3,with the new cool string formatting f-string.
>>> print(f"Sammy has {4:4} red and {16:16}! blue balloons")
Sammy has    4 red and               16! blue balloons

>>> # f-strings support any Python expressions inside the curly braces
>>> name = 'f-string'
>>> print(f"My cool string is called {name.upper()}.")
My cool string is called F-STRING.

>>> a, b = 5, 7
>>> f'{a}/{b} = {a/b:.2}'
'5/7 = 0.71'

>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~^20}')
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~AWESOME~~~~~~~
(Jul-27-2017, 05:27 PM)snippsat Wrote: [ -> ]When we taking about new string formatting .format() has been available for a while(9-year).
it's still new to me.  even though i have been doing python for about 6 years, doing it for others for about 4 years, hassling you guys with stupid questions on it for about 2 years, i am still in "learning mode" with python, especially stuff i didn't see in previous languages (like C, awk, and pike).  the string.format() method doesn't seem to get much coverage in newbie documents, so i suspect a lot of python programmers (especially those with a previous language experience) missed it.  not counting the Wide Wild World of modules there are probably dozens of things i've missed up to now.  i expect i'll be in learning mode with python for at least another decade or so.  i was in learning mode with C for at least 3 decades.
Pages: 1 2