As the documentation stated:

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

textwrap only split words at word breaks (usually at spaces) and makes sure that each string doesn't go over the width limit. Note: it doesn't split at every width characters.

It is used to splitting strings into sections, with each section having the most possible number of words equal or less than width characters long, while not breaking any of the words.

Answer from Taku on Stack Overflow
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί textwrap.html
textwrap β€” Text wrapping and filling
wrap(), fill() and shorten() work by creating a TextWrapper instance and calling a single method on it.
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_module_textwrap.asp
Python textwrap Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import textwrap text = 'This is a long message from Tobias that needs to be wrapped.' wrapped = textwrap.fill(text, width=30) print(wrapped) Try it Yourself Β»
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί textwrap-text-wrapping-filling-python
Textwrap – Text wrapping and filling in Python - GeeksforGeeks
May 31, 2017 - Python Β· import textwrap sample_text = """This function wraps the input paragraph such that each line n the paragraph is at most width characters long. The wrap method returns a list of output lines.
🌐
Note.nkmk.me
note.nkmk.me β€Ί home β€Ί python
Wrap and Truncate a String with textwrap in Python | note.nkmk.me
January 28, 2024 - print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~')) # Python can be easy to pick up whether # you're a first time programmer or ~
🌐
Martin Heinz
martinheinz.dev β€Ί blog β€Ί 108
Everything You Can Do with Python's textwrap Module | Martin Heinz | Personal Website & Blog
February 7, 2024 - We can also use the fill function which is a shorthand for "\n".join(wrap(text, ...)). The difference between the 2 is that wrap will give us a list of lines that we would need to concatenate ourselves, and fill gives us a single string that's already joined using newlines. textwrap module ...
🌐
HackerRank
hackerrank.com β€Ί challenges β€Ί text-wrap β€Ί tutorial
Text Wrap | HackerRank
>>> import textwrap >>> string = "This is a very very very very very long string." >>> print textwrap.fill(string,8) This is a very very very very very long string.
🌐
Python
docs.python.org β€Ί 3.6 β€Ί library β€Ί textwrap.html
6.4. textwrap β€” Text wrapping and filling β€” Python 3.6.15 documentation
In particular, fill() accepts exactly the same keyword arguments as wrap(). ... Collapse and truncate the given text to fit in the given width. First the whitespace in text is collapsed (all whitespace is replaced by single spaces). If the result fits in the width, it is returned.
Find elsewhere
🌐
Python
docs.python.org β€Ί 3.1 β€Ί library β€Ί textwrap.html
7.5. textwrap β€” Text wrapping and filling β€” Python v3.1.5 documentation
Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70. ... Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. fill() is shorthand for
🌐
Coderz Column
coderzcolumn.com β€Ί tutorials β€Ί python β€Ί textwrap-simple-guide-to-wrap-and-fill-text-in-python
textwrap - Simple Guide to Wrap and Fill Text in Python by Sunny Solanki
March 6, 2021 - Our code for this example starts by creating an instance of TextWrapper. It then prints the value of all attributes of the instance. It then calls wrap() and fill() methods on instance to explain their usage.
🌐
TutorialsPoint
tutorialspoint.com β€Ί python-text-wrapping-and-filling
Python Text Wrapping and Filling
July 30, 2019 - This tutorial gives enough understanding on Python programming language.""" my_wrap = textwrap.TextWrapper(width = 40) wrap_list = my_wrap.wrap(text=python_desc) for line in wrap_list: print(line) single_line = """Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.""" print('\n\n' + my_wrap.fill(text = single_line)) short_text = textwrap.shorten(text = python_desc, width=150) print('\n\n' + my_wrap.fill(text = short_text))
🌐
Beautiful Soup
tedboy.github.io β€Ί python_stdlib β€Ί generated β€Ί generated β€Ί textwrap.fill.html
textwrap.fill() β€” Python Standard Library
Fill a single paragraph of text, returning a new string. Reformat the single paragraph in β€˜text’ to fit in lines of no more than β€˜width’ columns, and return a new string containing the entire wrapped paragraph. As with wrap(), tabs are expanded and other whitespace characters converted ...
🌐
YouTube
youtube.com β€Ί watch
Python [textwrap] 01 Wrap() and Fill() - YouTube
If you would like to support me, please like, comment & subscribe, and check me out on Patreon: https://patreon.com/johnhammond010E-mail: johnhammond010@gmai...
Published Β  December 18, 2014
🌐
Python
docs.python.org β€Ί 3.0 β€Ί library β€Ί textwrap.html
textwrap β€” Text wrapping and filling β€” Python v3.0.1 documentation
Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70. ... Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. fill() is shorthand for
🌐
Python
docs.python.org β€Ί 3.9 β€Ί library β€Ί textwrap.html
textwrap β€” Text wrapping and filling β€” Python 3.9.21 documentation
wrap(), fill() and shorten() work by creating a TextWrapper instance and calling a single method on it.
🌐
Python Morsels
pythonmorsels.com β€Ί wrapping-text
Wrapping text output in Python - Python Morsels
December 2, 2025 - Any keyword argument that can be passed to the textwrap.TextWrapper class can also be passed to fill or wrap. The TextWrapper class allows for customizing indentation, where lines are broken when wrapping, whitespace normalization, and more.
🌐
Runebook.dev
runebook.dev β€Ί en β€Ί docs β€Ί python β€Ί library β€Ί textwrap β€Ί textwrap.TextWrapper.fill
python - From fill() to Format: Handling Newlines and Indentation in textwrap
The standard textwrap.fill() function (which is a convenience function that does the same thing as creating a default TextWrapper and calling fill()) works perfectly here, but you need to iterate.
🌐
Python Module of the Week
pymotw.com β€Ί 2 β€Ί textwrap
textwrap – Formatting text paragraphs - Python Module of the Week
import textwrap from textwrap_example import sample_text print 'No dedent:\n' print textwrap.fill(sample_text) ... $ python textwrap_fill.py No dedent: The textwrap module can be used to format text for output in situations where pretty-printing is desired.
🌐
Chennai Mathematical Institute
cmi.ac.in β€Ί ~madhavan β€Ί courses β€Ί prog2-2015 β€Ί docs β€Ί python-3.4.2-docs-html β€Ί library β€Ί textwrap.html
6.4. textwrap β€” Text wrapping and filling β€” Python 3.4.2 documentation
In particular, fill() accepts exactly the same keyword arguments as wrap(). ... Collapse and truncate the given text to fit in the given width. First the whitespace in text is collapsed (all whitespace is replaced by single spaces). If the result fits in the width, it is returned.