Python Glossary Русский

all Python 3textwrapfunction

textwrap.wrap

wrapping

Wrap text into a list of lines each at most `width` characters.

Syntax

textwrap.wrap(text, width=70)

width counts characters, and a word longer than width is chopped in the middle by default — painful for URLs and long file paths; break_long_words=False keeps the word intact at the cost of a line that overflows width. The result is a list of lines with no newline characters in them, so joining is up to you.

Examples

import textwrap
print(textwrap.wrap('a b c d', width=3))   # → ['a b', 'c d']
print(textwrap.wrap('one two three four', width=9))   # → ['one two', 'three', 'four']
print(textwrap.wrap('abcdefghij', width=4))   # → ['abcd', 'efgh', 'ij']
print(textwrap.wrap('abcdefghij', width=4, break_long_words=False))   # → ['abcdefghij']
print(textwrap.wrap('', width=10))   # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy