I observed this strange behaviour of center method of str objects in python 2.7 –
>>> print '-'.center(5,'.') # '-' is the minus or hyphen key on the keyboard.
..-.. # correctly centered
>>> print '─'.center(5, '.') # '─' is NOT the minus or hyphen key on the keyboard.
.─. # incorrectly centered
Is this a bug in the implementation of center method or has something to do with the use of unicode characters?
How do I fix this?
[Note that this works perfectly fine in python 3.2]
In Python 2, there’s a distinction between the
strandunicodetypes. Astris a sequence of bytes and is created with un-modified quotes. Your'─'is creating a sequence of 3 bytes which display as a single unicode character; when you askstr.center()to center those bytes in a field 5 bytes wide, it adds a single byte on either side; when printed you only get 3 glyphs.If you create a
unicodeobject (withu'─'or ‘─’.decode(‘utf-8’)), you now have a sequence of 1 unicode character, which will be centered by adding 2 characters to either side.Python 3 doesn’t have this problem, because ordinary strings are unicode character sequences.