Python Glossary Русский

Python 3.3+textwrapfunction

textwrap.indent

indentation

Add a prefix to the beginning of selected lines of the text.

Syntax

textwrap.indent(text, prefix)

By default only lines that contain non-whitespace get the prefix, so blank lines stay blank instead of turning into lines of trailing spaces. Pass a predicate as the third argument to decide line by line. It does no wrapping at all, only prepending; the opposite operation is textwrap.dedent.

Examples

import textwrap
print(textwrap.indent('hi', '>> '))   # → >> hi
print(repr(textwrap.indent('a\nb', '    ')))   # → '    a\n    b'
print(repr(textwrap.indent('a\n\nb', '# ')))   # → '# a\n\n# b'
print(repr(textwrap.indent('a\n\nb', '# ', lambda line: True)))   # → '# a\n# \n# b'
print(repr(textwrap.indent('', '> ')))   # → ''

See also

Open in the full glossarydocs.python.orgReport an inaccuracy