Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #198067

Re: ''.join([bin(i)[2:].zfill(8) for i in flag])

Subject Re: ''.join([bin(i)[2:].zfill(8) for i in flag])
Newsgroups comp.lang.python
References <nnd$45c195ac$47172e6f@35c50d4e9a8c02a9> <bin-20260924000855@ram.dialup.fu-berlin.de>
From Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Organization Watcom Pro Ltd.
Message-ID <qnwtS.229030$RI1.198964@fx04.ams4> (permalink)
Date 2026-09-25 23:25 +0800

Show all headers | View raw


On 9/24/2026 7:09 AM, Stefan Ram wrote:
> Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted:
>> As the topic says, is
>>    ''.join([bin(i)[2:].zfill(8) for i in flag])
>> really the most Pythonic way to do this?
> . . .
>> I mean, isn't there a regular print-to-string-format function, something
>> akin to C's sprintf(), that does this in a more succinct way?
> 
>    Like,
> 
> ''.join( f'{i:08b}' for i in flag )
> 
>    ?
> 
>> Here, `flag` is a string variable, if it's not clear from the con-
>> text.
> 
>    If "flag" would be a string, then "i" would be a string too,
>    but "bin" expects an integer.

Mea culpa, as Julius Caesar would say it.  I missed the fact that the
string in question has a b in front of it, like this.

   b'this is some string.'

And that indeed turns the string into a sequence of bytes.  So much to
learn, so little Python.


Best wishes, and happy Python.
-- 
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

''.join([bin(i)[2:].zfill(8) for i in flag]) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-24 06:42 +0800
  Re: ''.join([bin(i)[2:].zfill(8) for i in flag]) ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-23 23:09 +0000
    Re: format_binary Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-09-24 02:01 +0000
      Re: format_binary Jon Ribbens <jon+usenet@unequivocal.eu> - 2026-09-24 06:57 +0000
    Re: ''.join([bin(i)[2:].zfill(8) for i in flag]) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-25 23:25 +0800

csiph-web