Jaraco Text Utilities
jaraco.text is a Python library offering a collection of various text manipulation utilities, including advanced text wrapping, line joining, block indentation, symbol substitution, and more. It is actively maintained with a regular release cadence, currently at version 4.2.0.
Warnings
- breaking Version 4.0.0 and above dropped support for Python versions older than 3.9. Users on Python 3.8 or earlier must use jaraco.text version 3.x.
- gotcha The `wrap` function in `jaraco.text` is distinct from the standard library's `textwrap.wrap`. While similar in concept, it has different parameters (e.g., `indent` instead of `initial_indent`/`subsequent_indent`) and may behave differently.
- gotcha Many utilities like `join_lines` or `ellipsis_middle` are nested within submodules (e.g., `jaraco.text.join`, `jaraco.text.compress`) and must be imported from their specific paths, not directly from `jaraco.text`.
Install
-
pip install jaraco.text
Imports
- wrap
from jaraco.text import wrap
- join_lines
from jaraco.text.join import join_lines
- ellipsis_middle
from jaraco.text.compress import ellipsis_middle
Quickstart
from jaraco.text import wrap
from jaraco.text.join import join_lines
from jaraco.text.compress import ellipsis_middle
# Example 1: Custom text wrapping with indentation
long_text = "This is a very long paragraph that demonstrates the custom wrapping functionality provided by jaraco.text. It can handle various widths and indentation levels."
wrapped = wrap(long_text, width=40, indent=4)
print("--- Wrapped Text ---")
print(wrapped)
# Example 2: Joining lines, preserving relative indentation
lines_to_join = [
" First indented line",
" Second deeper line",
"Third top-level line"
]
joined = join_lines(lines_to_join)
print("\n--- Joined Lines ---")
print(joined)
# Example 3: Shorten string with ellipsis in the middle
very_long_string = "The quick brown fox jumps over the lazy dog and then takes a nap under the shade tree."
shortened = ellipsis_middle(very_long_string, max_len=30)
print("\n--- Ellipsis Middle ---")
print(shortened)