w3resource

Python: Wrap a given string into a paragraph of given width

Python String: Exercise-82 with Solution

Write a Python program to wrap a given string into a paragraph with a given width.

Sample Solution:

Python Code:

# Import textwrap module
import textwrap 

# Get input string
s = input("Input a string: ")

# Get width for wrapping  
w = int(input("Input the width of the paragraph: ").strip())

# Print result 
print("Result:")

# Wrap input string to width w
print(textwrap.fill(s,w)) 

Sample Output:

Input a string:  The quick brown fox.
Input the width of the paragraph:  10
Result:
The quick
brown fox.

Flowchart:

Flowchart: Wrap a given string into a paragraph of given width.

Python Code Editor:

Previous: Write a Python program to find the index of a given string at which a given substring starts. If the substring is not found in the given string return 'Not found'.
Next: Write a Python program to print four values decimal, octal, hexadecimal (capitalized), binary in a single line of a given integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-82.php