Rezha Julio

The Hard Coded Chemist

Format text paragraphs with textwrap

Python’s textwrap module is useful for rearranging text, e.g. wrapping and filling lines. Import the module: import textwrap Wrap the text in the string “parallel”, so that all lines are a maximum of x characters long: # When x = 2 textwrap.wrap("parallel", width=2) # Output: # ['pa', 'ra', 'll', 'el'] # When x = 4 textwrap.wrap("parallel", width=4) # Output: # ['para', 'llel'] Returns a list of lines (without trailing newlines). Continue reading