Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #198067
| Path | csiph.com!news.nrbbs.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!fx04.ams4.POSTED!not-for-mail |
|---|---|
| MIME-Version | 1.0 |
| User-Agent | Mozilla Thunderbird |
| 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> |
| Content-Language | en-US |
| From | Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> |
| Organization | Watcom Pro Ltd. |
| In-Reply-To | <bin-20260924000855@ram.dialup.fu-berlin.de> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Lines | 36 |
| Message-ID | <qnwtS.229030$RI1.198964@fx04.ams4> (permalink) |
| X-Complaints-To | abuse@easynews.com |
| X-Complaints-Info | Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly. |
| Date | Fri, 25 Sep 2026 23:25:10 +0800 |
| X-Received-Bytes | 2121 |
| Xref | csiph.com comp.lang.python:198067 |
Show key headers only | 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
''.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