Formatting A Text Based On Number Of Columns In Python

Shweta Lodha
2 min readAug 16, 2022

Say you have a text which is very long and occupying many columns or, in other words, say you have a text which is so long that the end user is seeing scrollbar because of the limited screen size.

Now, when the text is very long and user viewable area is limited, then as a programmer we have to do some adjustments to pull down some of the text to the next line.

Well, it feels daunting, but it is very easy to achieve this in Python.

Problem Statement

How to re-format a long text so that it can fit into the user-provided number of columns?

Solution

Like every other Python application, here we need to import the required module and that is textwrap:

import textwrap

Setting Number Of Columns

Once package is imported, we need to call function fill(…) as shown below:

text = “Welcome to Medium! I hope you are enjoying reading and writing articles on this platform.”print(textwrap.fill(text,40))

--

--