Alignment with string.format and unicode

4

I'm having trouble aligning strings when using .format() , for example when doing:

>>> print '{:>6}'.format('agua'}
  agua

>>> print '{:>6}'.format('água'}
 água

Note that the first format comes out as expected, 2 spaces + 4 characters = 6. In the second this does not happen, there is 1 space + 4 characters, since the size of this string unicode is larger than which we visually see

>>> len('agua')
4
>>> len('água')
5

This is 'breaking' the whole alignment of my output, is there any way to fix this?

    
asked by anonymous 21.09.2016 / 16:55

1 answer

2

In Python 2.x , use the prefix u to represent the string as Unicode :

>>> print '{:>6}'.format('agua')
  agua
>>> print u'{:>6}'.format(u'água')
  água
>>> 
    
21.09.2016 / 18:14