print format for binary representation

Jp Calderone exarkun at intarweb.us
Tue Jan 27 10:10:10 EST 2004


On Tue, Jan 27, 2004 at 07:00:23AM -0800, Rim wrote:
> Hi,
> 
> >>> print '%x' % 54
> 36
> >>> print '%b' % 54
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: unsupported format character 'b' (0x62) at index 1
> >>>
> 
> No formating string for binary? There %x for hex. Would %b make sense
> for bin? Is this worth a PEP?
> 
> How do I get 00110110 printed instead of the traceback?

  Oft requested, oft rejected.  Defining binary() is about one line of code. 
Hex is supported because printf(3) supports it.

  Here's a version to toy with:

  binary = lambda i, c = (lambda i, c: i and (c(i >> 1, c) + str(i & 1)) or ''): c(i, c)

  Jp





More information about the Python-list mailing list