3

I'm trying to convert a string coming from raw_input() into a "string of bytes". When I type the variable manually (in the code) it works fine, as it returns me a length of 5. However, when I try to enter the "string of bytes" with raw_input() it returns me a length of 20.

>>> x='\xB2\xB2\xB3\xB4\x01'
>>> len(x)
5
>>> x=raw_input()
\xB2\xB2\xB3\xB4\x01
>>> len(x)
20

I would like to know why this is happening and how can I fix it. Thanks in advance.

3
  • 1
    raw_input() is 'raw' in the sense that the text is not parsed or escaped before being interpreted as a string. When you assign a text string to a variable at the command prompt, the text is first processed by the python interpreter so that special characters can be converted to the appropriate representation. Commented Feb 22, 2016 at 15:58
  • raw_input() treats your input literary as ['\', 'x', 'B', '2' ... ]. String and array of bytes is same thing in Python 2.7. Commented Feb 22, 2016 at 15:59
  • Would you ever actually enter "\x..." as input? Wouldn't you rather be entering "²²³´", which would internally result in the same thing as your first x? Commented Feb 22, 2016 at 16:01

1 Answer 1

1

When you submit the string "\xB2\xB2\xB3\xB4\x01" to raw_input() it automatically escapes the \ characters because it thinks you mean to enter them as part of a string. This results in the representation of the string to read like this:

In [2]: x=raw_input()
\xB2\xB2\xB3\xB4\x01

In [3]: x
Out[3]: '\\xB2\\xB2\\xB3\\xB4\\x01'

In [4]: print x
\xB2\xB2\xB3\xB4\x01

Unfortunately the answer to your question is basically that you shouldn't be manually entering a string of bytes to raw_input().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.