Skip to main content

Unicode Width Approximation

A library for calculating the display width of Unicode strings in terminal/monospace environments.

Installation

pip install unicode-width-approximation

Usage

from unicode_width_approximation import get_string_width, get_codepoint_width

# Get width of strings
print(get_string_width("hello"))      # 5
print(get_string_width("中文"))        # 4
print(get_string_width("👨‍👩‍👧‍👦"))  # 2

# Get width of single code points
print(get_codepoint_width(ord('A')))  # 1
print(get_codepoint_width(0x4E00))    # 2 (CJK)
print(get_codepoint_width(0x1F600))   # 2 (emoji)

API

get_string_width(s: str) -> int

Calculate the total display width of a UTF-8 encoded string.

get_codepoint_width(code: int) -> int

Get the display width of a single Unicode code point (0, 1, or 2).

is_wide_char(code: int) -> bool

Check if a code point is a wide character (East Asian Wide or Fullwidth).

is_zero_width(code: int) -> bool

Check if a code point is a zero-width character.

Width Rules

Character Type Width
Control characters (Cc) 0
Format characters (Cf) 0
Combining marks (Mn, Me) 0
East Asian Wide (W) 2
East Asian Fullwidth (F) 2
Emoji_Presentation 2
Other characters 1

License

MIT License

Metadata

Release files for unicode-width-approximation 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unicode-width-approximation 1.0.0
File Size Uploaded
unicode_width_approximation-1.0.0.tar.gz 7.1 kB Details

Release files / unicode_width_approximation-1.0.0.tar.gz

Download URL unicode_width_approximation-1.0.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9dfb7a8e922a14a8b631ab0efc99d1206f5d1b211122d5b593e56a9afe0a5030
BLAKE2b-256 checksum
How to use checksums
bbfbd4373b8da9d409c7f8aa0c88a76267405c4d3bfd90814dde81088ce3f2c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page