- len()は文字列の長さを返すため、全角も半角も同じ1文字でカウントする
- 文字列の幅を求める基準としては使えない
east_asian_width()の戻り値
| 戻り値 | 説明 |
| F | Fullwidht:全角文字 |
| H | Halfwidht:半角文字 |
| W | Wide:全角文字 |
| Na | Narrow:半角文字 |
| A | Ambiguous:全角文字 |
| N | Neutral:半角文字 |
unicodedataモジュールのeast_asian_widht()で文字列の幅を調べられる- 文字種が全角ならwidhtを+2、それ以外ならwidhtを+1すればよい
import unicodedata
def get_east_asian_width(text):
width = 0
for char in text:
if unicodedata.east_asian_width(char) in ('F', 'W', 'A'):
width += 2
else:
width += 1
return width
get_east_asian_width("文字列abc")
# 9