On this page
  1. stringprep
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Checking Prohibited Control Characters
  7. Gotchas & Boundaries
  8. Common Functions
  9. Constants & Attributes

stringprep

Prev: string | Up: stdlib_module | Next: struct
Rel: string, re, difflib, textwrap, unicodedata
Ref: docs.python.org — stringprep
Tags: #public #python3 #stdlib


Import

import stringprep

Overview

RFC 3454 Stringprep Unicode character-classification tables (in_table_a1(), in_table_c12(), map_table_b1(), etc.) used to normalize and validate internationalized protocol identifiers (such as legacy IDNA2003 domain names, SASL, and XMPP). Use stringprep only when implementing an RFC that explicitly cites RFC 3454; for general Unicode normalization, use unicodedata.

Quick Start

import stringprep

# Table C.1.2 checks for non-ASCII space characters (e.g., U+00A0 NO-BREAK SPACE)
assert stringprep.in_table_c12("\u00a0") is True
assert stringprep.in_table_c12(" ") is False

Implementation Walkthrough

1. Checking Prohibited Control Characters

Protocols using Stringprep reject characters in tables like C.2.1 (ASCII control characters) or C.2.2 (non-ASCII control characters):

import stringprep

def has_prohibited_controls(text: str) -> bool:
    return any(stringprep.in_table_c21(ch) or stringprep.in_table_c22(ch) for ch in text)

assert has_prohibited_controls("hello\x00world") is True
assert has_prohibited_controls("hello world") is False

Gotchas & Boundaries

Common Functions

Constants & Attributes

Linked from

Related notes