How to concatenate strings with binary values ​​in python?

What is the easiest way in python to concatenate a string with binary values?

sep = 0x1
data = ["abc","def","ghi","jkl"]

Search for result data "abc0x1def0x1ghi0x1jkl"with 0x1 being a binary value, not the string "0x1".

+3
source share
2 answers

I think,

joined = '\x01'.join(data) 

must do it. \x01is an escape sequence for a byte with a value of 0x01.

+9
source

The chr () function will have the effect of translating the variable into a string with the binary value you are looking for.

>>> sep = 0x1
>>> sepc = chr(sep)
>>> sepc
'\x01'

The join () function can then be used to concatenate a string of strings with your binary value as a separator.

>>> data = ['abc']*3
>>> data
['abc', 'abc', 'abc']
>>> sepc.join(data)
'abc\x01abc\x01abc'
+3
source

Source: https://habr.com/ru/post/1710756/


All Articles