textwrap provides functions for word wrapping and filling text.
This can be very useful in commandline programs where you want to format dynamic output nicely so it looks good in a terminal.
To wrap text, one must know the width of each word so can know when to break lines. This library measures the width of text using the displayed width, not the size in bytes.
This is important for non-ASCII text. ASCII characters such as
! are simple: the displayed with is the same as the number
of bytes used in their UTF-8 encoding (one ASCII character takes
up one byte in UTF-8). However, non-ASCII characters and symbols
take up more than one byte:
0xc3 0xa9 and
0xe2 0x9a 0x99 in UTF-8, respectively.
This is why we take care to use the displayed width instead of the byte count when computing line lengths. All functions in this library handle Unicode characters like this.
A Wrapper holds settings for wrapping and filling text.
Removes common leading whitespace from each line.
Fill a line of text at
Add prefix to each non-empty line.
Wrap a line of text at