{"id":4280,"library":"textwrap3","title":"textwrap3","description":"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.","status":"maintenance","version":"0.9.2","language":"en","source_language":"en","source_url":"https://github.com/jonathaneunice/textwrap3","tags":["text processing","backport","python2","python3","text wrapping","formatting"],"install":[{"cmd":"pip install textwrap3","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.","symbol":"wrap","correct":"from textwrap3 import wrap"},{"note":"Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.","symbol":"fill","correct":"from textwrap3 import fill"},{"note":"This function, backported from Python 3.4+, is available directly from 'textwrap3'.","symbol":"shorten","correct":"from textwrap3 import shorten"},{"note":"The TextWrapper class for advanced configuration is imported directly.","symbol":"TextWrapper","correct":"from textwrap3 import TextWrapper"}],"quickstart":{"code":"from textwrap3 import wrap, fill, shorten\n\nlong_text = \"\"\"This is a very long string that needs to be wrapped \ninto 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.\"\"\"\n\n# Using wrap to get a list of lines\nwrapped_lines = wrap(long_text, width=40)\nprint(\"Wrapped lines (list):\")\nfor line in wrapped_lines:\n    print(line)\n\nprint(\"\\n\" + \"-\" * 30 + \"\\n\")\n\n# Using fill to get a single string with newlines\nfilled_text = fill(long_text, width=40)\nprint(\"Filled text (single string):\")\nprint(filled_text)\n\nprint(\"\\n\" + \"-\" * 30 + \"\\n\")\n\n# Using shorten to truncate text\nshortened_text = shorten(long_text, width=50, placeholder=' [...]')\nprint(\"Shortened text:\")\nprint(shortened_text)","lang":"python","description":"Demonstrates basic text wrapping, filling, and shortening using the core functions from the textwrap3 library."},"warnings":[{"fix":"Be aware of potential Unicode handling discrepancies. Test text wrapping behavior thoroughly with diverse character sets, especially when migrating from native Python 2.x `textwrap`.","message":"Due to its design principle, `textwrap3` adheres to Python 3's sensibilities, especially concerning Unicode. It uses `re.UNICODE` and treats Unicode em-dashes like ASCII '--'. This can lead to subtle differences in wrapping behavior compared to the native `textwrap` module in older Python 2.x environments where Unicode handling might differ.","severity":"gotcha","affected_versions":"All versions of textwrap3, specifically when used in Python 2.x environments."},{"fix":"Pass `replace_whitespace=False` to `wrap()` or `TextWrapper` constructor if you want to prevent the collapsing of whitespace. For multi-paragraph text, it's often better to split the text into paragraphs, wrap each, and then join them back.","message":"The `wrap()` and `fill()` functions, by default, replace all internal whitespace characters (including newlines and tabs) with single spaces before wrapping. Existing newlines in multi-paragraph input will be collapsed. If you need to preserve original newlines as paragraph breaks, process paragraphs separately or use `replace_whitespace=False` (though this might lead to other formatting challenges).","severity":"gotcha","affected_versions":"All versions of textwrap3."},{"fix":"For Python 3.6 and later, use the built-in `textwrap` module directly (e.g., `import textwrap`) instead of `textwrap3`. The functionality is identical.","message":"For modern Python 3.6+ environments, `textwrap3` is generally unnecessary. All the features it backports are already present in the standard library's `textwrap` module. Installing it in these environments adds an unnecessary dependency.","severity":"gotcha","affected_versions":"Users on Python 3.6 and newer."},{"fix":"If you encounter performance issues with very long words, consider setting `break_on_hyphens=False` or `break_long_words=False` in the `TextWrapper` constructor or `wrap`/`fill` function calls if breaking long words is not critical, or pre-processing the text to insert break points.","message":"The underlying `textwrap.wrap()` logic (inherited by `textwrap3`) can exhibit poor performance (quadratic time complexity) when wrapping text containing extremely long, unbroken words (e.g., a very long string without spaces or hyphens) if `break_long_words` is `True` (the default).","severity":"gotcha","affected_versions":"All versions of textwrap3."}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}