textwrap3

0.9.2 · maintenance · verified Sat Apr 11

textwrap3 is a compatibility back-port of Python 3.6's built-in `textwrap` module, providing its features (like `shorten` and `max_lines`) to older Python versions from 2.6 forward. It also includes minor tweaks, particularly around Unicode handling and em-dashes. The current version is 0.9.2, released in January 2019, indicating a maintenance-focused release cadence.

Warnings

Install

Imports

Quickstart

Demonstrates basic text wrapping, filling, and shortening using the core functions from the textwrap3 library.

from textwrap3 import wrap, fill, shorten

long_text = """This is a very long string that needs to be wrapped 
into several lines to be more readable. It demonstrates the basic functionality of the textwrap3 library, which is a backport of Python 3.6's textwrap module."""

# Using wrap to get a list of lines
wrapped_lines = wrap(long_text, width=40)
print("Wrapped lines (list):")
for line in wrapped_lines:
    print(line)

print("\n" + "-" * 30 + "\n")

# Using fill to get a single string with newlines
filled_text = fill(long_text, width=40)
print("Filled text (single string):")
print(filled_text)

print("\n" + "-" * 30 + "\n")

# Using shorten to truncate text
shortened_text = shorten(long_text, width=50, placeholder=' [...]')
print("Shortened text:")
print(shortened_text)

view raw JSON →