Python: Wrap a given string into a paragraph of given width
Wrap string into paragraph with width.
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:

For more Practice: Solve these Related Problems:
- Write a Python program to wrap a given string into lines of a specified width using the textwrap module.
- Write a Python program to implement custom logic that breaks a long string into multiple lines without splitting words.
- Write a Python program to use the fill() function from textwrap to format text into a paragraph of fixed width.
- Write a Python program to accept a paragraph and a width from the user and output the text wrapped accordingly.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics