
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I wrap a string in a file in Python?
Wrapping the string means formatting or breaking the string so that it fits within the specified width. It is useful when writing the content to the file, such as logs or console-like outputs.
Python provides various ways to achieve string wrapping before writing it to the file using the built-in methods. In this article, we are going to learn how to wrap a string in a file in Python.
Using Python textwrap.wrap() Method
The textwrap.wrap() method is used to wrap a single paragraph of text. so that every line is at most a specified length. It accepts a text and a width value as parameters. Following is the syntax of the Python textwrap.wrap() method -
textwrap.wrap(text, width)
To wrap a string in a file, we need to use the textwrap.wrap() function and split the string into multiple lines, and write each line into the file using a loop, followed by the newline character.
Example
Let's look at the following example, where we are going to wrap the string into lines of 5 characters and write it to a file.
import textwrap str1 = "Welcome to TutorialsPoint, The E-learning platform" result = textwrap.wrap(str1, width=5) with open("demo1.txt", "w") as file: for line in result: file.write(line + "\n") print("Text wrapping to 'demo1.txt' completed successfully.")
The output of the above program is as follows -
Text wrapping to 'demo1.txt' completed successfully.
Below is the wrapping of the text that is visible in the file after running the above program.
demo1.txtWelcome to TutorialsPoint, The E-learning platform
Using Python textwrap.fill() Method
The Python textwrap.fill() method is part of the textwrap module, which provides the function used for formatting and wrapping text.
The fill() method is used to format a single paragraph of text so that it fits in the specified width by inserting line breaks where required.
Syntax
Following is the syntax of the Python textwrap.fill() method -
textwrap.fill(text, width)
Example
Consider another scenario, where we are going to perform the wrapping using the textwrap.fill() method for direct string formatting.
import textwrap str1 = "Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace." result = textwrap.fill(str1, width=22) with open("demo2.txt", "w") as f: f.write(result) print("Text wrapping to 'demo2.txt' completed successfully.")
The output of the above program is as follows -
Text wrapping to 'demo2.txt' completed successfully.
Below is the wrapping of the text that is visible in the file after running the above program.
demo2.txtTutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace.