it_is_a_literature it_is_a_literature - 1 year ago 45
Python Question

Checking a character is fullwidth or halfwidth in Python

I want to check whether a character is fullwidth or halfwidth using Python

string="你好hallo"
for char in string:
if( \uFF60- \u0f01 and \uFFE0-\uFFE6 ): print( char +"is fullwidth")
elif(\uFF61-\uFFDC and \uFFE8-\uFFEE):print(char+ " is halfwidth")


Please help me to change this pseudocode into real python code.

Answer Source

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

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download