If you have a use-case where you want to wrap text that you output using the Python code on the console or a Jupyter Notebook, you can make use of the textwrap module.
We can make use of the width attribute to set the width of each line where you want to wrap the text.
Examples:
import textwrap
text = """This is a long sentence that I would like to wrap for this Python Program output. Let's see if it works."""
text_wrapper = textwrap.TextWrapper(width=15)
wrapped_text_list = text_wrapper.wrap(text=text)
for wrapped_line in wrapped_text_list:
print(wrapped_line)


You can expore more about this module here: https://docs.python.org/3/library/textwrap.html
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!