This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: allow string as sep in _Py_strhex_impl ( bytearray.hex() )
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, arne123, gregory.p.smith
Priority: normal Keywords:

Created on 2022-03-17 21:03 by arne123, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg415447 - (view) Author: arne123 (arne123) Date: 2022-03-17 21:03
I use Python to support some C development.
Often I need to convert bytearrays to C like convention:
0x12, 0x34

It would be very convenient for this use case if the separator could be a string (like ", 0x") instead of just a single character.
msg415450 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-17 21:23
Would there be substantial benefit of a new feature over using the existing feature and then calling str.replace()?

>>> b = b'abracadabra'
>>> "0x" + b.hex(":").replace(":", ", 0x")
'0x61, 0x62, 0x72, 0x61, 0x63, 0x61, 0x64, 0x61, 0x62, 0x72, 0x61'
msg415451 - (view) Author: arne123 (arne123) Date: 2022-03-17 21:30
Well, I think there are many ways to solve this in python (e.g. I used iteration before, wasn't aware of the sep. at all), but when there is already a separator, why limiting it to one character?
msg415452 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-17 21:45
In particular, it's one ascii character:

    >>> b'abracadabra'.hex('😋')
    ValueError: sep must be ASCII.

I wouldn't be completely opposed to allowing longer strings, but since there are easy enough ways to do it already, put me at a -0. We can see if anyone else is in favor.
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91208
2022-03-17 21:45:08Dennis Sweeneysetversions: + Python 3.11
nosy: + gregory.p.smith

messages: + msg415452

components: + Interpreter Core, - IO
2022-03-17 21:30:40arne123setmessages: + msg415451
2022-03-17 21:23:01Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg415450
2022-03-17 21:03:45arne123create