The fill() function

The fill() function works similarly to textwrap.wrapexcept it returns the data joined into a single, newline-separated string. This function wraps the input in text and returns a single string containing the wrapped text.

The syntax for this function is: 

textwrap.fill(text, width)
  • text: Text to wrap.

  • width: Maximum length allowed of a wrapped line. The default value is 70.

Now, we will see an example of fill(). Create a fill_example.py script and write the following content in it:

import textwrap

sample_string = '''Python is an interpreted high-level programming language.'''

w = textwrap.fill(text=sample_string, width=50)
print(w)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 fill_example.py
Python is an interpreted high-level programming
language.

In the preceding example, we used the fill() function. The procedure is the same as what we did in wrap(). First, we created a string variable. Next, we created the textwrap object. Then, we applied the fill() function. Finally, we printed the output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset