خرید بک لینک

Vote count: 0

I was wondering why this works, casting a string consisting of a number following line breaks, like '5', 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

asked 2 mins ago

1 Answer

Vote count: 0

Because the interpreter evaluates the whitespaces beforehand:

In [137]: int(' r n 5 r n ')
Out[137]: 5

This won't work if you make the string raw:

In [138]: int(r' r n 5 r n ')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-138-bd46e2cccbca> in <module>()
----> 1 int(r' r n 5 r n ')

ValueError: invalid literal for int() with base 10: ' \r \n 5 \r \n '

answered 10 secs ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 28 ارديبهشت 1395 ساعت: 8:42

صفحه بندی