Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e1bc7c

Browse files
committed
whatsnew: textwrap.shorten.
Also add the missing TextWrapper.shorten method doc.
1 parent a183064 commit 6e1bc7c

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Doc/library/textwrap.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,13 @@ hyphenated words; only then will long words be broken if necessary, unless
282282

283283
Wraps the single paragraph in *text*, and returns a single string
284284
containing the wrapped paragraph.
285+
286+
287+
.. method:: shorten(text, width=70, *, placeholder=" [...]")
288+
289+
Collapse and truncate the given text to fit in the given width.
290+
The text first has its whitespace collapsed. If it then fits in
291+
the *width*, it is returned unchanged. Otherwise, as many words
292+
as possible are joined and then the *placeholder* is appended.
293+
294+
.. versionadded:: 3.4

Doc/whatsnew/3.4.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,20 @@ especially if automated via a test suite. (Contributed by Antoine Pitrou
12101210
in :issue:`13390`.)
12111211

12121212

1213+
textwrap
1214+
--------
1215+
1216+
:mod:`textwrap` has a new function :func:`~textwrap.shorten`, implemented via a
1217+
new :class:`~textwrap.TextWrapper` method
1218+
:meth:`~textwrap.TextWrapper.shorten`, that provides a convenient way to create
1219+
a string that displays just the leading porting of an input string. It
1220+
collapses any whitespace, truncates the result to a specified width, and a
1221+
specified placeholder is added (by default, ``[...]``, stored in the new
1222+
:attr:`~textwrap.TextWrapper.placeholder` attribute of the
1223+
:class:`~textwrap.TextWrapper` object)). (Contributed by Antoine Pitrou in
1224+
:issue:`18585`.)
1225+
1226+
12131227
traceback
12141228
---------
12151229

0 commit comments

Comments
 (0)