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

    From Johann 'Myrkraverk' Oskarsson@3:633/10 to All on Thu Sep 24 06:42:16 2026
    Dear comp.lang.python,

    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, why are we converting
    each character in the flag [1] to a binary string, chunk off the first
    two bytes, and then fill up the front with zeros [2], just to join it
    with an empty string?

    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? Can we do something like

    flag.expand_to_binary() ## ?

    Here, it should be obvious that `flag` is indeed of the A.S.C.I.I. sub-
    set, but this doesn't seem like it would work well, if the text is Uni-
    code and U.T.F.-8.

    This is a single line from the Cryptohack competition, but I am assuming
    the authors of the website are cryptographers, and not expert Pythonics.


    What do you think?

    [1] Here, `flag` is a string variable, if it's not clear from the con-
    text.

    [2] Or is it the back, as I haven't used zfill() before?
    --
    Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
    I'm not from the Internet, I just work there. | via XS News https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Johann 'Myrkraverk' Oskarsson@3:633/10 to All on Fri Sep 25 23:25:10 2026
    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

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)