Python Glossary Русский

all Python 3textwrapfunction

textwrap.dedent

indentation

Remove common leading whitespace from all lines of the text.

Syntax

textwrap.dedent(text)

Only the prefix common to every non-blank line is removed: if a single line has no indentation — typically text started right after the opening triple quote — there is no common prefix and nothing is stripped, so begin such literals with a newline. Tabs and spaces are compared literally and are not treated as equivalent, so mixed indentation quietly defeats dedent; run expandtabs first. Whitespace-only lines are ignored when computing the prefix and come out empty.

Examples

import textwrap
print(textwrap.dedent('    hello'))   # → hello
print(textwrap.dedent('    a\n      b').splitlines())  # → ['a', '  b']
print(textwrap.dedent('  a\n   \n  b').splitlines())  # → ['a', '', 'b']
print(textwrap.dedent('a\n  b').splitlines())  # → ['a', '  b']
print(textwrap.dedent('   ').splitlines())  # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy