1

sorry new to python, I have done a couple of searches but failed to get the answer I wanted.

So let's say I have a string '3918f33a' but I want my final output to equal to:

output ='\x3a\xf3\x18\x39'

so when I print(output) I will get:

:ó▒9

how do I go about doing that? I have tried to combine "\x"+string[6:8] .. but it will only error out.

Qifan Guo
  • 1,344
  • 1
  • 8
  • 10

1 Answers1

2

You might want to use some appropriate slicing within a for loop or list comprehension; an example:

a = '3918f33a'
output = ''.join(r'\x' + a[i-2:i] for i in range(len(a), 0, -2))
print(output) # \x3a\xf3\x18\x39
j1-lee
  • 13,764
  • 3
  • 14
  • 26
  • Hi lee thanks for the fast response, it is my fault that I wasn't been fully clear on the question. Yes, the output would print the \x3a\xf3\x18\x39, but I actually want the output variable to actually equal to '\x3a\xf3\x18\x39'. right now the value of the output variable is actually '\\x3a\\xf3\\x18\\x39' – Qifan Guo Oct 03 '21 at 18:56