Textwraping in Python

You can use the ‘textwrap’ module to format and output text to design pretty outputs. If provides convenience methods to perform text wraping like you see in word processors.

import textwrap

sample_text = '''There is a wide, yawning black infinity. In every direction, the extension is endless; the sensation of depth is overwhelming. And the darkness is immortal. Where light exists, it is pure, blazing, fierce; but light exists almost nowhere, and the blackness itself is also pure and blazing and fierce.'''

print(textwrap.fill(sample_text, width=40))

Leave a Reply

Your email address will not be published. Required fields are marked *