FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
PythonText validation & encoding

Build a URL query string

Python: Use urlencode to percent-encode query parameter values correctly.

Python✓ Tested
PythonText validation & encoding

Compare Unicode text without case

Python: Casefold handles more Unicode case mappings than lower for caseless comparison.

Python✓ Tested
PythonText validation & encoding

Convert hexadecimal text to bytes

Python: Use bytes.fromhex for a readable byte representation and hex for the reverse conversion.

Python✓ Tested
PythonText validation & encoding

Encode and decode binary data as Base64

Python: Use Base64 when a text format must carry bytes; it is encoding, not encryption.

Python✓ Tested
PythonText validation & encoding

Escape text for HTML content

Python: Use html.escape before placing plain text in an HTML text or quoted attribute context.

Python✓ Tested
PythonText validation & encoding

Extract named regex fields

Python: Use named capture groups to parse a known date-shaped identifier.

Python✓ Tested
PythonText validation & encoding

Normalize equivalent Unicode strings

Python: Normalize to NFC before comparing canonically equivalent text.

Python✓ Tested
PythonText validation & encoding

Remove a known prefix safely

Python: Use removeprefix instead of lstrip when removing a literal prefix rather than a set of characters.

Python✓ Tested
PythonText validation & encoding

Remove a known suffix safely

Python: Use removesuffix to remove only a matching ending.

Python✓ Tested
PythonText validation & encoding

Validate an entire identifier

Python: Use fullmatch so a regex must consume the complete input.

Python✓ Tested