The snippet library.
Small, practical examples. Search by task or narrow things down below.
Build a URL query string
Python: Use urlencode to percent-encode query parameter values correctly.
Python✓ Tested
Compare Unicode text without case
Python: Casefold handles more Unicode case mappings than lower for caseless comparison.
Python✓ Tested
Convert hexadecimal text to bytes
Python: Use bytes.fromhex for a readable byte representation and hex for the reverse conversion.
Python✓ Tested
Encode and decode binary data as Base64
Python: Use Base64 when a text format must carry bytes; it is encoding, not encryption.
Python✓ Tested
Escape text for HTML content
Python: Use html.escape before placing plain text in an HTML text or quoted attribute context.
Python✓ Tested
Extract named regex fields
Python: Use named capture groups to parse a known date-shaped identifier.
Python✓ Tested
Normalize equivalent Unicode strings
Python: Normalize to NFC before comparing canonically equivalent text.
Python✓ Tested
Remove a known prefix safely
Python: Use removeprefix instead of lstrip when removing a literal prefix rather than a set of characters.
Python✓ Tested
Remove a known suffix safely
Python: Use removesuffix to remove only a matching ending.
Python✓ Tested
Validate an entire identifier
Python: Use fullmatch so a regex must consume the complete input.
Python✓ Tested