Pragmatic string utilities for APIs and data cleaning
Project description
stringextn
A pragmatic, zero-dependency Python library for practical string manipulation and text cleaning. stringextn provides battle-tested utilities for case conversion, HTML/emoji removal, substring matching, fuzzy comparison, security masking, URL slug generation, and multi-string replacement—all designed with real-world edge cases in mind.
Installation
Install via pip:
pip install stringextn
Requires Python 3.8 or higher. No external dependencies.
Quick Start
from stringextn import (
to_snake, to_camel, to_pascal, to_kebab,
clean_text, remove_html, remove_emoji,
contains_any, contains_all,
similarity,
multi_replace,
mask_email, mask_phone,
slugify
)
# Case conversion
to_snake("myVariableName") # "my_variable_name"
to_camel("my_variable_name") # "myVariableName"
to_pascal("my-variable-name") # "MyVariableName"
to_kebab("myVariableName") # "my-variable-name"
# Text cleaning
clean_text("<p>Hello & goodbye!</p>") # "Hello & goodbye!"
remove_html("<div>Content</div>") # "Content"
remove_emoji("Hello 👋 World 🌍") # "Hello World "
# Substring matching
contains_any("hello world", ["world", "foo"]) # True
contains_all("hello world", ["hello", "world"]) # True
# Fuzzy string matching
similarity("kitten", "sitting") # 0.615
# Multi-replace
multi_replace("abc abc abc", {"a": "X", "b": "Y"}) # "XYc XYc XYc"
# Privacy masking
mask_email("user@example.com") # "u***@example.com"
mask_phone("5551234567") # "****4567"
# URL-safe slugs
slugify("Hello, World! ✨") # "hello-world"
Features
Case Conversion
to_snake(s)– Converts to snake_caseto_camel(s)– Converts to camelCaseto_pascal(s)– Converts to PascalCaseto_kebab(s)– Converts to kebab-case
Supports mixed input formats (camelCase, PascalCase, kebab-case, snake_case, space-separated).
Text Cleaning
clean_text(s)– Comprehensive cleaning pipeline: HTML entity unescaping, tag removal, emoji removal, Unicode normalization, and whitespace normalizationremove_html(s)– Strips HTML/XML tagsremove_emoji(s)– Removes emoji charactersnormalize_spaces(s)– Collapses whitespace and trimsnormalize_unicode(s)– Applies NFKD normalization for consistent character representation
Substring Operations
contains_any(s, items)– Returns True if string contains any itemcontains_all(s, items)– Returns True if string contains all items
Case-sensitive substring matching using Python's in operator.
Fuzzy Matching
similarity(a, b)– Returns similarity score (0.0–1.0) using difflib's SequenceMatcher- 1.0 = identical strings
- 0.0 = no similarity
- Rounded to 3 decimal places
String Replacement
multi_replace(s, mapping)– Performs simultaneous multi-string replacement- All keys are treated as literal strings (regex special chars auto-escaped)
- Non-cascading: each substring is replaced exactly once
Security & Privacy
mask_email(email)– Hides all but first character of email local part- Format:
u***@example.com - Raises
ValueErrorif email doesn't contain exactly one@
- Format:
mask_phone(phone)– Hides all but last 4 digits- Format:
****1234
- Format:
URL Slugs
slugify(s)– Generates URL-safe slugs- Cleans text, lowercases, replaces non-alphanumeric with hyphens
- Strips leading/trailing hyphens
- Example:
"Hello, World! ✨"→"hello-world"
Performance & Behavior Notes
Unicode Handling
- NFKD Normalization: The
clean_text()andslugify()functions apply NFKD (Compatibility Decomposition) normalization, which:- Decomposes accented characters (é → e + ´)
- Applies compatibility mappings (fi → fi)
- Ensures consistent character representation across different input encodings
- Emoji removal uses Unicode ranges and handles most emoticons and symbols; complex emoji sequences (skin tones, zero-width-joiner) may not be fully removed
- Non-ASCII characters in
to_snake()andto_camel()are preserved but not affected by case conversion
Edge Cases
- Empty strings: Most functions return empty strings;
contains_all("", [])returns True (vacuous truth) - Whitespace: Leading/trailing whitespace is preserved in case conversion; use
normalize_spaces()first if needed - Consecutive separators:
multi_replace()andslugify()handle consecutive delimiters correctly (collapsed in slugs, replaced individually in multi_replace) - Special regex characters:
multi_replace()automatically escapes all regex special characters in mapping keys - Email masking: No format validation; only checks for single
@symbol - Phone masking: Works with any string; no validation of format
Performance
- All functions use compiled regular expressions or built-in operations for efficiency
- No external dependencies; pure Python implementation
- Suitable for high-volume text processing in APIs and data pipelines
Testing
Run the test suite with pytest:
pytest tests/
License
MIT License. See LICENSE file for details.
Contributing
Contributions are welcome. Please ensure all tests pass and add tests for new functionality.
GitHub: stringextn
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stringextn-1.0.4.tar.gz.
File metadata
- Download URL: stringextn-1.0.4.tar.gz
- Upload date:
- Size: 41.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a809f92c016b17a2867532a3cea40e6ed78950da042368f34aa9d2e80643d9d9
|
|
| MD5 |
04b185c8640dcc489dbfa22202370446
|
|
| BLAKE2b-256 |
a200e7b5a2aa7c3ebb945e2132e5839dc6d6ad2145c39baf46fe5b3523a6a823
|
File details
Details for the file stringextn-1.0.4-py3-none-any.whl.
File metadata
- Download URL: stringextn-1.0.4-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
992cb0950616fec29ffd797ff3a2db54800cc04094162f4e33399043105e98c7
|
|
| MD5 |
d74f2765f3f4e33c7399674aa4e0a8e6
|
|
| BLAKE2b-256 |
dd2ce3167f717cb047f2c681b607303fbd2b36b1974cd475a86dc9aa13d6e894
|