

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 to write multiple lines in text file using Python?
You can use write function to write multiple lines by separating lines by '\n'.
For example
line1 = "First line" line2 = "Second line" line3 = "Third line" with open('my_file.txt','w') as out: out.write('{}\n{}\n{}\n'.format(line1,line2,line3))
Alternatively, you can use writelines function to write these lines.
For example
line1 = "First line" line2 = "Second line" line3 = "Third line" with open('my_file.txt','w') as out: out.writelines([line1, line2, line3])
- Related Questions & Answers
- How to write a single line in text file using Python?
- Counting number of lines in text file using java
- How to write a text file in Selenium with python?
- Python - Write multiple files data to master file
- How to write text and output it as a text file using R?
- How to display multiple lines of text in Tkinter Label?
- How to count the number of lines in a text file using Java?
- How to write the plot title in multiple lines using plot function in R?
- Java program to delete duplicate lines in text file
- How can multiple lines be visualized using Bokeh Python?
- How do we use file.readlines() to read multiple lines using Python?
- How to write binary data to a file using Python?
- How to match pattern over multiple lines in Python?
- Number of Lines To Write String in Python
- How to search and replace text in a file using Python?
Advertisements