Write a Python program to add a prefix text to all of the lines in a string


The program uses the textwrap module in Python to process and format text. The text is stored in the para string and has multiple lines and indents. The textwrap.dedent function is used to remove the common indentation from the text stored in para. The resulting text without indentation is stored in the without_Indent variable.

The textwrap.fill function is then used to wrap the text stored in without_Indent to a specified width of 70 characters. The resulting text is stored in the txt variable. Finally, the textwrap.indent function is used to add a specified prefix to the start of each line in the text stored in txt. In this case, the prefix is '* ', which is added to each line of text stored in txt. The resulting text with the added prefix is stored in the res variable and is printed to the console.

Source Code

import textwrap
para = """
	Python is an interpreted, object-oriented programming languages. 
	high-level programming language that can be used for a wide variety of applications. 
	Python is a powerful general-purpose programming language.
	First developed in the late 1980s by Guido van Rossum. 
	Python is open source programming language.
	Guido van Rossum named it after the BBC Comedy TV series Monty Python’s Flying Circus
"""
without_Indent = textwrap.dedent(para)
txt = textwrap.fill(without_Indent, width=70)
res = textwrap.indent(txt, '* ')
print(res)

Output


        Python is an interpreted, object-oriented programming languages.
        high-level programming language that can be used for a wide variety of applications.
        Python is a powerful general-purpose programming language.
        First developed in the late 1980s by Guido van Rossum.
        Python is open source programming language.
        Guido van Rossum named it after the BBC Comedy TV series Monty Python’s Flying Circus


Python is an interpreted, object-oriented programming languages.
high-level programming language that can be used for a wide variety of applications.
Python is a powerful general-purpose programming language.
First developed in the late 1980s by Guido van Rossum.
Python is open source programming language.
Guido van Rossum named it after the BBC Comedy TV series Monty Python’s Flying Circus


C:\Users\tutor\Desktop\String>17_Add_Prefix_Text_All_Lines_in_String.py
*  Python is an interpreted, object-oriented programming languages.
* high-level programming language that can be used for a wide variety of
* applications.  Python is a powerful general-purpose programming
* language. First developed in the late 1980s by Guido van Rossum.
* Python is open source programming language. Guido van Rossum named it
* after the BBC Comedy TV series Monty Python’s Flying Circus


Example Programs