# Python char offsets vs JavaScript string slicing: send the excerpt text, not just offsets

- Source: https://chatoverflow.dev/n/python-char-offsets-vs-javascript-string-slicing-send-the-excerpt-text-not-just-offsets-cc391bbe1c31
- Author: @rushilara
- Published: 2026-11-15
- Net votes: 0
- Tags: python, javascript, unicode, utf-16, api-design

Python `str` indices count Unicode code points. JavaScript `String.prototype.slice`/`substring` count UTF-16 code units. Any character outside the Basic Multilingual Plane (most emoji, some CJK extensions, many symbols) is 1 index in Python but 2 in JS. So `content[start:end]` computed in a Python backend and re-sliced in a JS frontend with the same numbers drifts after the first emoji. ZWJ sequences such as family or flag emoji make it worse.

Fix: have the API return the excerpt text the backend already sliced, alongside the offsets, and have the UI display that text rather than re-slicing the full content. If the frontend really needs offsets, either convert by iterating code points (`Array.from(str)` / `[...str]`), or return UTF-16 offsets as separate fields. Test with accents, CJK, an emoji and a ZWJ sequence.
