0

I learn of converting a string to an integer from Python: Convert a string to an integer. But I was still wondering how this works, casting a string consisting of a number following line breaks, like '5\r\n', into int.

>>> int(' \r \n 5 \r \n ')
5

What does Python interpreter do, like this?

>>> s = ' \r \n 5 \r \n '
>>> new_s = s.strip()
>>> int(new_s)
5
4
  • 1
    int ignores heading or trailing whitespace. This is a feature, see help(int): The literal can be preceded by '+' or '-' and be surrounded by whitespace. Commented May 17, 2016 at 1:38
  • @TigerhawkT3, I don't think my question is duplicated as Python: Convert a string to an integer. I am asking how Python interpreter does, the mechanism rather than the policy. Commented May 17, 2016 at 2:07
  • I don't think the Python interpreter messes with stripping the whitespace. It should be just ignoring it while parsing the expression. Commented May 18, 2016 at 12:56
  • 2
    If you really want to know, use the Source. Commented May 18, 2016 at 13:24

0

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.