How to control utf-8 string paddings

Posted by Kev on Stack Overflow See other posts from Stack Overflow or by Kev
Published on 2011-01-07T03:50:10Z Indexed on 2011/01/07 3:53 UTC
Read the original article Hit count: 159

Filed under:

I got three UTF-8 stings:

hello, world
hello, ??
hello, ?rld

I only want the first 10 chars so that the bracket in one column:

[hello, wor]
[hello, ? ]
[hello, ?r]

In console:

width('??')==width('worl')
width('? ')==width('wor')  #a white space behind '?'

python's format() doesn't help when UTF-8 chars mixed in

>>> for s in ['[{0:<{1}.{1}}]'.format(s, 10) for s in ['hello, world', 'hello, ??', 'hello, ?rld']]:
...    print(s)
...
[hello, wor]
[hello, ?? ]
[hello, ?rl]

So, I wonder if there is a standard way to do the UTF-8 padding staff?

© Stack Overflow or respective owner

Related posts about python