Skip to main content

Truncate strings intelligently without cutting words

Project description

philiprehberger-string-truncate

Tests PyPI version Last updated

Truncate strings intelligently without cutting words.

Installation

pip install philiprehberger-string-truncate

Usage

from philiprehberger_string_truncate import truncate, truncate_middle, truncate_path

truncate("Hello beautiful world", max_length=15)
# "Hello…"

truncate("Hello beautiful world", max_length=15, suffix="...")
# "Hello..."

truncate_middle("Hello beautiful world of code", max_length=20)
# "Hello bea…l of code"

truncate_path("/very/long/path/to/some/file.txt", max_length=25)
# "/very/long/.../file.txt"

Word-count truncation

from philiprehberger_string_truncate import truncate_words

truncate_words("one two three four five", max_words=3)
# "one two three..."

truncate_words("one two", max_words=5)
# "one two"

Wrapping

from philiprehberger_string_truncate import wrap

wrap("the quick brown fox jumps over the lazy dog", width=20)
# "the quick brown fox\njumps over the lazy\ndog"

wrap("first paragraph\n\nsecond paragraph", width=80)
# "first paragraph\n\nsecond paragraph"

API

Function / Class Description
truncate(text, max_length, suffix="…", break_words=False) Word-boundary truncation
truncate_middle(text, max_length, separator="…") Keep start and end
truncate_path(path, max_length, separator="/", placeholder="...") Path-aware truncation
truncate_words(text, max_words, suffix="...") Truncate to a word count, appending suffix when shortened
wrap(text, width=80) Wrap to lines at word boundaries; preserves paragraph breaks

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_string_truncate-0.2.0.tar.gz (177.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file philiprehberger_string_truncate-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_string_truncate-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7ee39487a30bb9bce8fa998c8a0e6d96d4c7769694b363aea403cef6b6e7bf25
MD5 b31ac3dae322886d6b1e4f384e9c2890
BLAKE2b-256 12edaae1bc0ce16d40b1eaf6e5eca27766ce1b1fc971de9df8a876487b553484

See more details on using hashes here.

File details

Details for the file philiprehberger_string_truncate-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_string_truncate-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd65cc2a10ec8cb5e8097fd0deadcb59597c0f914ae889513d100f6936a73e9a
MD5 a1dfdc3f7a1ed3174f7cb50aef63f7dd
BLAKE2b-256 9ede37ac8af1d8af2a8d60072a727ec18eee2859590bdd7e235db99d051bcb57

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page