🔵 🔵 🔵


Primary

၊၊||၊|။

textwrap.indent() ⚬|Documentation|1st|20260122195929-00-⌔

textwrap — Text wrapping and filling — Python 3.14.2 documentation#textwrap.indent

textwrap.indent(text, prefix, predicate=None)

Add prefix to the beginning of selected lines in text.

Lines are separated by calling text.splitlines(True).

By default, prefix is added to all lines that do not consist solely of whitespace (including any line endings).

For example:

>>> s = 'hello\n\n \nworld'
>>> indent(s, '  ')
'  hello\n\n \n  world'

The optional predicate argument can be used to control which lines are indented. For example, it is easy to add prefix to even empty and whitespace-only lines:

>>> print(indent(s, '+ ', lambda line: True))
+ hello
+
+
+ world

Added in version 3.3.

wrap(), fill() and shorten() work by creating a TextWrapper instance and calling a single method on it. That instance is not reused, so for applications that process many text strings using wrap() and/or fill(), it may be more efficient to create your own TextWrapper object.

Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words; only then will long words be broken if necessary, unless TextWrapper.break_long_words is set to false.

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •