summaryrefslogtreecommitdiffstats
path: root/Doc/library/textwrap.rst
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-06-11 13:07:51 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-06-11 13:07:51 (GMT)
commit4fae8cdaeac117c6a567f9305a22fd0cff400452 (patch)
treef01b63d1495c581d36e0f346cb8fa23a8b160045 /Doc/library/textwrap.rst
parent3c4acd8bf9b7f2e1dcfd5bb76c4562451c3d8fa1 (diff)
downloadcpython-4fae8cdaeac117c6a567f9305a22fd0cff400452.zip
cpython-4fae8cdaeac117c6a567f9305a22fd0cff400452.tar.gz
cpython-4fae8cdaeac117c6a567f9305a22fd0cff400452.tar.bz2
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Berch)
Diffstat (limited to 'Doc/library/textwrap.rst')
-rw-r--r--Doc/library/textwrap.rst35
1 files changed, 31 insertions, 4 deletions
diff --git a/Doc/library/textwrap.rst b/Doc/library/textwrap.rst
index a74789c..50c710c 100644
--- a/Doc/library/textwrap.rst
+++ b/Doc/library/textwrap.rst
@@ -12,7 +12,7 @@
The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and
:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work,
-and a utility function :func:`dedent`. If you're just wrapping or filling one
+and two utility functions, :func:`dedent` and :func:`indent`. If you're just wrapping or filling one
or two text strings, the convenience functions should be good enough;
otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
@@ -45,9 +45,10 @@ Text is preferably wrapped on whitespaces and right after the hyphens in
hyphenated words; only then will long words be broken if necessary, unless
:attr:`TextWrapper.break_long_words` is set to false.
-An additional utility function, :func:`dedent`, is provided to remove
-indentation from strings that have unwanted whitespace to the left of the text.
-
+Two additional utility function, :func:`dedent` and :func:`indent`, are
+provided to remove indentation from strings that have unwanted whitespace
+to the left of the text and to add an arbitrary prefix to selected lines
+in a block of text.
.. function:: dedent(text)
@@ -72,6 +73,32 @@ indentation from strings that have unwanted whitespace to the left of the text.
print(repr(dedent(s))) # prints 'hello\n world\n'
+.. function:: 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
+
+
.. class:: TextWrapper(**kwargs)
The :class:`TextWrapper` constructor accepts a number of optional keyword