- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to wrap a text into paragraph with width w
Suppose we have a string s and width w. We have to wrap this text into a paragraph with width w. This can be done very easily with fill() function present inside textwrap library. So we have to import textwrap library first.
So, if the input is like s = "The quick brown fox jumps over the lazy dog" w = 9, then the output will be
The quick
brown fox
jumps
over the
lazy dog
To solve this, we will follow these steps −
take the string into s
take width into w
call textwrap.fill(s, w) by passing s as the first argument, and w as the second argument
Example
Let us see the following implementation to get better understanding
import textwrap def solve(s, w): return textwrap.fill(s, w) s = "The quick brown fox jumps over the lazy dog" w = 9 print(solve(s, w))
Input
"The quick brown fox jumps over the lazy dog", 9
Output
The quick brown fox jumps over the lazy dog
- Related Articles
- Indent the text of a paragraph with CSS
- Align the text of a paragraph with CSS
- How to wrap the text within the width of the window in JavaFX?
- Java Program to wrap text in a JTextPane and show Scrollbar
- Calculate text width with JavaScript
- How to word-wrap text in Tkinter Text?
- How to read text file into a list or array with Python?
- How to wrap the text of a label in JavaFX?
- How to wrap the text in text flow layout in JavaFX?
- Add some emphasis to a paragraph with Bootstrap
- How can we implement line wrap and word wrap text inside a JTextArea in Java?
- Set the text wrap in a form in HTML
- How to wrap each input line to fit in specified width in Linux?
- Wrap Strings of Text in Bootstrap Navbar
- Program to find maximum width ramp in Python

Advertisements