Write A Python Program To Wrap A Given String Into A Paragraph Of Given Width

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Wrap A Given String Into A Paragraph Of Given Width

 

Title : 

Write A Python Program To Wrap A Given String Into A Paragraph Of Given Width 

 

Write A Python Program To Wrap A Given String Into A Paragraph Of Given Width




 

Description : 

Hi guys, in this article you will learn a python program That takes an input as a string and creates a string into a paragraph of specified width. The width of the paragraph is given by the user as input. Take three variables: first variable to store a string from the user, next variable is used to ask a user to enter the width of the paragraph, and the third variable is to store the string in paragraph form.


    
text = input("Enter a string : ")
width = int(input("Enter Width of Paragraph : "))
para = ""


    


Now using the if statement check if the given weight of paragraph is greater than the length of the given input string then initialize the paragraph to text. Now using the else block use the for loop to enumerate all the letters present in the given string as well as index. Use the elif block to check if the index is completely divisible by the given user width then add the new line letter to the paragraph health keep adding the letter to paragraph.


    
if width > len(text):
   para = text
else:
   for index, letter in enumerate(text):
       if index % width == 0:
           para += "
"
       para += letter

print("The paragraph formed is ...")
print(para)


    



    

    




Complete Python Code : 


    
text = input("Enter a string : ")
width = int(input("Enter Width of Paragraph : "))
para = ""

if width > len(text):
   para = text
else:
   for index, letter in enumerate(text):
       if index % width == 0:
           para += "
"
       para += letter

print("The paragraph formed is ...")
print(para)



    

 

 

Output : 


    
Enter a string : hello world
Enter Width of Paragraph : 5
The paragraph formed is ...

hello
 worl
d



    

 

Output : 


    
Enter a string : code with tj
Enter Width of Paragraph : 3
The paragraph formed is ...

cod
e w
ith
 tj




    



Conclusion :

The above Python program asks the user to enter the string and width of the paragraph. Then it processes the given string and computes the paragraph appropriately to the given width. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments