Home | Blag Index


From: wayne+blog@waynewerner.com
To: everyone.everywhere.all.at.once
Date: Mon, 21 Apr 2025 09:30:19 -0500
Subject: Does Python Ignore Whitespace On `int`?

Basically what it says on the tin. I had a question so let's find the answer:

>>> int('42')
42
>>> int('42   ')
42
>>> int('42   \n')
42
>>> int('42   \n\t\v')
42
>>> int('    42   \n\t\v')
42
>>> int('    100   \n\t\v')
100
>>> int('    100   \n\t\v', base=2)
4

So... yes!

>>> lines = '''
... 100
... 001
... 010
... 111
... '''
>>> for line in lines.strip().splitlines():
...     print(int(line, base=2))
...
4
1
2
7

~Wayne

^C


Home | Blag Index

This site is Copyleft Wayne Werner - BY-NC-SA 4.0