1
$\begingroup$

I have a list, which looks like this:

params
['h\x00i\x00', '\x00t\x00h\x00e\x00r\x00e\x00']

Now, all I want is to merge these two elements into the string "hi there", but when I use

print ' '.join(params)

I get

h i  t h e r e

How do I strip out those hex characters to make the string "hi there"?

$\endgroup$

2 Answers 2

3
$\begingroup$

You have the null equivalent hexadecimal '\x00' in between each character. To remove those hexadecimal characters, first join the list with a space and then split it at '\x00' and then join again. Print statement simply looks like follows.

>>> print(' '.join( ' '.join(params).split('\x00')))
$\endgroup$
1
  • $\begingroup$ Hello and welcome to the site. To make the spaces more clear, consider formatting your answer. To format as code (monospace) add four spaces in front of the code line. Consider the help pages for further suggestions. $\endgroup$ Commented Feb 28, 2019 at 8:25
1
$\begingroup$

Do the following:

print ''.join(params)

You have added an extra space. Omit it.

Edit:

It seems I've forgotten to mention that you have to split the '\x00' sequence from your string. Consequently, you have to use the following code:

print "".join(params).split('\x00')
$\endgroup$
3
  • 1
    $\begingroup$ Please look at the question more carefully and test your answers. Your answer does not address the question at all. $\endgroup$ Commented Feb 28, 2019 at 8:20
  • $\begingroup$ @mapto I open to criticism. Why do you think it can't solve the issue? $\endgroup$ Commented Feb 28, 2019 at 8:37
  • $\begingroup$ Because params is a list, not a string. See other answer for an actual solution. You practically apply only the last step of the transformation. $\endgroup$ Commented Feb 28, 2019 at 8:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.