The indent() function

The indent() function is used to add the specified prefix to the beginning of the selected lines in your text.

The syntax for this function is: 

            textwrap.indent(text, prefix)
  • text: The main string
  • prefix: The prefix to add

Create a indent_example.py script and write the following content in it:

import textwrap

str1 = "Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace."

w = textwrap.fill(str1, width=30)
i = textwrap.indent(w, '*')
print(i)

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

student@ubuntu:~/work$ python3 indent_example.py
*Python is an interpreted high-
*level programming language for
*general-purpose programming.
*Created by Guido van Rossum
*and first released in 1991,
*Python has a design philosophy
*that emphasizes code
*readability, notably using
*significant whitespace.

In the preceding example, we used the fill() and indent() functions of the textwrap module. First, we used the fill method to store the data into the w variable. Next, we used the indent method. Using indent(), each line in the output will have a * prefix. And next, 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