In Python 3, the pound symbol, £, is one character long, as you would expect.
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> len('£')
1
But in Python 2 (which I, like many people, have to use for work), it is two characters long.
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('£')
2
Unless you convert it to unicode.
In [152]: len(u'£')
Out[152]: 1
Worth knowing!
It's also worth being aware that 'str's get a bit funny in Python 2 when you wander out of ascii-land. And iterating through them will not do what you expect. I recommend sticking to unicode where you can.
No comments:
Post a Comment