user3584604
user3584604

Reputation: 119

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

Upvotes: 11

Views: 4161

Answers (2)

Simon Richter
Simon Richter

Reputation: 29618

My answer to a similar question:

You are looking for the rendering width from the current output context. For graphical UIs, there is usually a method to directly query this information; for text environments, all you can do is guess what a conformant rendering engine would probably do, and hope that the actual engine matches your expectations.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

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()

Upvotes: 2

Related Questions