ID EN
Text Wrapping

shorten

Python 3.11

Collapse and truncate the given text to fit in the given width.

Syntax

PYTHON
textwrap.shorten(text, width, *, fix_sentence_endings=False, break_long_words=True, break_on_hyphens=True, placeholder=' [...]')

Examples

Example 1
PYTHON
>>> textwrap.shorten("Hello  world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello  world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'

See Also

TextWrapper TextWrapper fill() tabsize expand_tabs drop_whitespace replace_whitespace