w3resource

Pandas Series: str.wrap() function

Series-str.wrap() function

The str.wrap() function is used to wrap long strings in the Series/Index to be formatted in paragraphs with length less than a given width.
This method has the same keyword parameters and defaults as textwrap.TextWrapper.

Syntax:

Series.str.wrap(self, width, **kwargs)
Pandas Series: str.wrap() function

Parameters:

Name Description Type/Default Value Required / Optional
width Maximum line width. int Required
expand_tabs If True, tab characters will be expanded to spaces (default: True). bool Optional
replace_whitespace If True, each whitespace character (as defined by string.whitespace) remaining after tab expansion will be replaced by a single space (default: True). bool Optional
drop_whitespace If True, whitespace that, after wrapping, happens to end up at the beginning or end of a line is dropped (default: True). bool Optional
break_long_words 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). bool Optional
break_on_hyphens 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). bool Optional

Returns: Series or Index

Note: 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

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['boy is taller', 'another boy is taller'])
s.str.wrap(11)

Output:

0            boy is\ntaller
1    another boy\nis taller
dtype: object

Previous: Series-str.upper() function
Next: Series-str.zfill() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-str-wrap.php