Checking a Character Is Fullwidth or Halfwidth in Python

You can check the width of the character using unicodedata.east_asian_width(unichr):

1
2
3
4
5
6
7
8
import unicodedata

for char in string:
status = unicodedata.east_asian_width(char)
if status == 'F':
print('{0} is full-width.'.format(char))
elif status == 'H':
print('{0} is half-width.'.format(char))

It has the following returned values:

1
2
3
4
5
6
7
8
# East_Asian_Width (ea)

ea ; A ; Ambiguous
ea ; F ; Fullwidth
ea ; H ; Halfwidth
ea ; N ; Neutral
ea ; Na ; Narrow
ea ; W ; Wide

Returned values of 'W', 'F' and 'A' should be considered as full-width on Windows.

Reference: http://www.unicode.org/reports/tr44/tr44-4.html#Validation_of_Enumerated

On POSIX platform, the quote characters (u'“' and u'”') are considered as ambiguous, which are actually 1 character width in console. For console usage, you may try a 3rd-party library urwid:

1
2
3
4
5
6
7
>>> from urwid.util import str_util
>>> str_util.get_width(ord(u'x'))
1
>>> str_util.get_width(ord(u'“'))
1
>>> str_util.get_width(ord(u'你'))
2