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

New in version 3.3.

doc_python
2016-10-07 17:44:28
Comments
Leave a Comment

Please login to continue.