String, along with the matching module-level functions, exposes lazy iterators over Unicode boundaries.
Each yields String views into the original buffer, so segmentation stays allocation-free, except utf8_codepoints, which yields int code points.
Method / function
Standard
Yields
utf8_codepoints(string)
scalar values
int code points; ill-formed bytes decode to U+FFFD, so iteration never raises.
utf8_graphemes(string, skip_empty=False)
UAX-29 grapheme clusters
user-perceived characters such as a base plus combining marks, or emoji ZWJ sequences.
utf8_wordbreaks(string, skip_empty=False)
UAX-29 word boundaries
all UAX-29 word segments (words and the separators between them; they tile).
content BETWEEN any Unicode delimiter (superset of whitespace).
utf8_delimiters(string, skip_empty=False)
punctuation/symbol/separator
the delimiter runs themselves (the separators).
Naming follows one rule: the bare name (newlines/whitespaces/delimiters) yields the separators, while split_* yields the content between them. skip_empty drops empty segments, and with_separators=True interleaves both losslessly, so concatenating the result reproduces the input.
import stringzilla as szassert list(sz.utf8_codepoints("AB")) == [65, 66]assert [str(g) for g in sz.String("a👍🏽b").utf8_graphemes()] == ["a", "👍🏽", "b"][str(w) for w in sz.utf8_wordbreaks("Hi, world")] # all UAX-29 segmentsassert sum(1 for _ in sz.String("first\nsecond\nthird").utf8_split_newlines()) == 3assert [str(t) for t in sz.utf8_split_whitespaces("foo bar baz", skip_empty=True)] == ["foo", "bar", "baz"]assert "".join(str(s) for s in sz.utf8_split_newlines("a\nb", with_separators=True)) == "a\nb"
utf8_count(string) counts Unicode characters rather than bytes, unlike len, which counts bytes, and Strings.utf8_count() counts every string of a collection in one pass:
These apply Unicode case folding, correctly handling one-to-many expansions such as German ß matching SS.
utf8_uncased_fold(text, validate=False) — return the case-folded UTF-8 string as bytes.
utf8_uncased_search(haystack, needle, start=0, end=len, validate=False) — index of the first uncased match, or -1.
For str inputs start/end and the result are codepoint offsets; for bytes inputs they are byte offsets.
utf8_uncased_matches(haystack, needle, include_overlapping=False) — iterate over all uncased matches, yielding each matched region as a String view whose length may differ from needle due to folding expansions; an empty needle matches at every codepoint boundary, the end included.
Pass validate=True to validate UTF-8 before processing.
import stringzilla as szassert sz.utf8_uncased_fold("HELLO") == b"hello"assert sz.utf8_uncased_fold("Straße") == b"strasse"assert sz.utf8_uncased_search("Hello World", "WORLD") == 6assert sz.utf8_uncased_order("hello", "HELLO") == 0assert [str(m) for m in sz.utf8_uncased_matches("Hello HELLO hello", "hello")] == ["Hello", "HELLO", "hello"]
These mirror unicodedata.normalize but operate on raw UTF-8 bytes.
utf8_norm(text, form, validate=False) — normalize to one of 'NFC', 'NFD', 'NFKC', 'NFKD', 'NFKC_Casefold'; returns bytes. 'NFKC_Casefold' is the Unicode toNFKC_Casefold, the keys search engines match on: NFKC with full case folding and without default-ignorable codepoints.
utf8_find_denormalized(text, form) — return the byte offset of the first codepoint that breaks the given normalization form, or None if the string is already fully normalized.
Uses SIMD-accelerated detection of all 7 Unicode newline characters plus CRLF.
Unlike splitlines(), this returns an iterator for memory-efficient processing.
Returns:
iterator: An iterator yielding the lines as String objects.
Parameters
text_StringLike
The string object.
skip_emptybool
Skip empty lines, defaulting to False.
with_separatorsbool
Interleave the separators losslessly, defaulting to False.
Uses SIMD-accelerated detection of all 7 Unicode newline characters plus CRLF.
Yields each newline separator; utf8_split_newlines() yields the lines between them.
Returns:
iterator: An iterator yielding the newline separators as String objects.
Uses SIMD-accelerated detection of all 25 Unicode White_Space characters.
Splits on each whitespace codepoint; pass skip_empty=True for str.split()-style tokens.
Returns:
iterator: An iterator yielding the non-whitespace tokens as String objects.
Parameters
text_StringLike
The string object.
skip_emptybool
Skip empty segments, defaulting to False.
with_separatorsbool
Interleave the separators losslessly, defaulting to False.
Capabilities to run, defaulting to the CPU's enabled ones.
Recognized whitespace
ASCII: TAB, LF, VT, FF, CR, SPACE
Latin-1: NEXT LINE, NO-BREAK SPACE
General Punctuation: EN/EM QUAD/SPACE, THIN SPACE, etc.
CJK: IDEOGRAPHIC SPACE (U+3000)
Examples
>>> sum(1 for _ in sz.String('foo bar baz').utf8_split_whitespaces())3
utf8_whitespacesCreate an iterator over Unicode whitespace separators, one per codepoint, CR+LF kept together.
Uses SIMD-accelerated detection of all 25 Unicode White_Space characters.
Yields each whitespace codepoint; utf8_split_whitespaces() yields the tokens between them.
Returns:
iterator: An iterator yielding the whitespace separators as String objects.
Capabilities to run, defaulting to the CPU's enabled ones.
Recognized whitespace
ASCII: TAB, LF, VT, FF, CR, SPACE
Latin-1: NEXT LINE, NO-BREAK SPACE
General Punctuation: EN/EM QUAD/SPACE, THIN SPACE, etc.
CJK: IDEOGRAPHIC SPACE (U+3000)
Examples
>>> sum(1 for _ in sz.String('foo bar').utf8_whitespaces())1
utf8_split_delimitersCreate an iterator over the fields, the content between Unicode delimiters.
Uses SIMD-accelerated detection of every punctuation (P*), symbol (S*), and
separator/whitespace (Z*) codepoint - the superset of utf8_split_whitespaces().
Splits on delimiters, one separator per codepoint, and yields the segments between them.
Returns:
iterator: An iterator yielding the non-delimiter segments as String objects.
Parameters
text_StringLike
The string object.
skip_emptybool
Skip empty segments, defaulting to False.
with_separatorsbool
Interleave the separators losslessly, defaulting to False.
Uses SIMD-accelerated detection of every punctuation (P*), symbol (S*), and
separator/whitespace (Z*) codepoint. Yields each delimiter codepoint;
utf8_split_delimiters() yields the fields between them.
Returns:
iterator: An iterator yielding the delimiter separators as String objects.