Python Forum
format spec %b is documented but does not work
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
format spec %b is documented but does not work
#1
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>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I don't see %b in the documentation (https://docs.python.org/2/library/stdtyp...operations).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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:"
Reply
#5
(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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Looks like some lint. So report it.
Reply
#7
(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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
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~~~~~~~
Reply
#10
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  issubclass() not as documented Skaperen 19 7,800 Mar-15-2021, 04:58 PM
Last Post: nilamo
  where is this documented? Skaperen 8 4,637 Feb-20-2018, 03:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020