Finally In the output, we see the wrapped text. The longest line in it, the third line, is exactly 50 characters long.
import textwrap
value = """The image in these opening lines evidently refers to a bird
knocking itself out, in full flight, against the outer surface of a glass
pane in which a mirrored sky, with its slightly darker
tint and slightly slower cloud,
presents the illusion of continued space."""
# Wrap this text.
list = textwrap.wrap(value, width=50)
# Print each line.
for element in list:
print(element)The image in these opening lines evidently refers
to a bird knocking itself out, in full flight,
against the outer surface of a glass pane in which
a mirrored sky, with its slightly darker tint and
slightly slower cloud, presents the illusion of
continued space.
Discussion. Other methods, such as fill() are helpful. This method does the same thing as textwrap.wrap except it returns the data joined into a single, newline-separated string.
Summary. With textwrap, we access effective text wrapping methods. With this helpful module we can often avoid writing our own line-breaking algorithms.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 17, 2024 (grammar).