Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get "actual" length of string in Unicode characters

given a character like "" (\xe2\x9c\xae), for example, can be others like "Σ", "д" or "Λ") I want to find the "actual" length that character takes when printed onscreen

for example

len("✮")
len("\xe2\x9c\xae")

both return 3, but it should be 1

like image 278
user3584604 Avatar asked Apr 29 '14 09:04

user3584604


1 Answers

You may try like this:

unicodedata.normalize('NFC', u'✮')
len(u"✮")

UTF-8 is an unicode encoding which uses more than one byte for special characters. Check unicodedata.normalize()

like image 171
Rahul Tripathi Avatar answered Sep 27 '22 21:09

Rahul Tripathi