Python textwrap Module
Example
Wrap text to fit within a specific width:
import textwrap
text = 'This is a long message from Tobias that needs to be wrapped.'
wrapped = textwrap.fill(text, width=30)
print(wrapped)
Try it Yourself »
Definition and Usage
The textwrap module provides functions for wrapping and formatting plain text.
Use it to format paragraphs, add indentation, or wrap long lines to fit within a specified width.
Members
| Member | Description |
|---|---|
| TextWrapper | Class for customizing text wrapping behavior. |
| dedent() | Remove common leading whitespace from lines. |
| fill() | Wrap text and return a single string. |
| indent() | Add prefix to each line in text. |
| shorten() | Collapse and truncate text to fit in given width. |
| wrap() | Wrap text and return a list of lines. |