Jaraco Text Utilities

4.2.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates three common utilities from jaraco.text: `wrap` for custom text wrapping, `join_lines` for combining lines while preserving indentation, and `ellipsis_middle` for shortening long strings by placing an ellipsis in the center.

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)

view raw JSON →