pandas.Series.str.wrap#

Series.str.wrap(width, expand_tabs=True, tabsize=8, replace_whitespace=True, drop_whitespace=True, initial_indent='', subsequent_indent='', fix_sentence_endings=False, break_long_words=True, break_on_hyphens=True, max_lines=None, placeholder=' [...]')[source]#

Wrap strings in Series/Index at specified line width.

This method has the same keyword parameters and defaults as

textwrap.TextWrapper.

Parameters:
widthint, optional

Maximum line width.

expand_tabsbool, optional

If True, tab characters will be expanded to spaces (default: True).

tabsizeint, optional

If expand_tabs is true, then all tab characters in text will be expanded to zero or more spaces, depending on the current column and the given tab size (default: 8).

replace_whitespacebool, optional

If True, each whitespace character (as defined by string.whitespace) remaining after tab expansion will be replaced by a single space (default: True).

drop_whitespacebool, optional

If True, whitespace that, after wrapping, happens to end up at the beginning or end of a line is dropped (default: True).

initial_indentstr, optional

String that will be prepended to the first line of wrapped output. Counts towards the length of the first line. The empty string is not indented (default: ‘’).

subsequent_indentstr, optional

String that will be prepended to all lines of wrapped output except the first. Counts towards the length of each line except the first (default: ‘’).

fix_sentence_endingsbool, optional

If true, TextWrapper attempts to detect sentence endings and ensure that sentences are always separated by exactly two spaces. This is generally desired for text in a monospaced font. However, the sentence detection algorithm is imperfect: it assumes that a sentence ending consists of a lowercase letter followed by one of ‘.’, ‘!’, or ‘?’, possibly followed by one of ‘”’ or “’”, followed by a space. One problem with this algorithm is that it is unable to detect the difference between “Dr.” in […] Dr. Frankenstein’s monster […] and “Spot.” in […] See Spot. See Spot run […] Since the sentence detection algorithm relies on string.lowercase for the definition of “lowercase letter”, and a convention of using two spaces after a period to separate sentences on the same line, it is specific to English-language texts (default: False).

break_long_wordsbool, optional

If True, then words longer than width will be broken in order to ensure that no lines are longer than width. If it is false, long words will not be broken, and some lines may be longer than width (default: True).

break_on_hyphensbool, optional

If True, wrapping will occur preferably on whitespace and right after hyphens in compound words, as it is customary in English. If false, only whitespaces will be considered as potentially good places for line breaks, but you need to set break_long_words to false if you want truly insecable words (default: True).

max_linesint, optional

If not None, then the output will contain at most max_lines lines, with placeholder appearing at the end of the output (default: None).

placeholderstr, optional

String that will appear at the end of the output text if it has been truncated (default: ‘ […]’).

Returns:
Series or Index

Notes

Internally, this method uses a textwrap.TextWrapper instance with default settings. To achieve behavior matching R’s stringr library str_wrap function, use the arguments:

  • expand_tabs = False

  • replace_whitespace = True

  • drop_whitespace = True

  • break_long_words = False

  • break_on_hyphens = False

Examples

>>> s = pd.Series(["line to be wrapped", "another line to be wrapped"])
>>> s.str.wrap(12)
0             line to be\nwrapped
1    another line\nto be\nwrapped
dtype: object