Write a String to a File in Python
In Python, you can write a string to a file using the write() method of a file object. The file must be opened in write mode ('w'), append mode ('a'), or exclusive creation mode ('x'). Let’s explore different ways to write a string to a file with examples.
Examples
1. Writing a String to a File Using write() Method
The write() method writes a string to a file. If the file exists, it will overwrite the content; otherwise, a new file will be created.
# Define the string to write
text = "Hello, this is a sample text."
# Open the file in write mode
with open("example1.txt", "w") as file:
file.write(text)
print("String written to file successfully.")
Explanation:
- The variable
textstores the string we want to write. - The
open()function is used to open the fileexample1.txtin write mode ('w'). - The
write()method of the file object writes the string to the file. - The
withstatement ensures the file is properly closed after writing.
Output:

The file example1.txt is created with the given content.

2. Writing Multiple Strings Using writelines()
The writelines() method writes a list of strings to a file.
# Define multiple lines as a list
lines = ["First line\n", "Second line\n", "Third line\n"]
# Open the file in write mode
with open("example2.txt", "w") as file:
file.writelines(lines)
print("Multiple lines written to file successfully.")
Explanation:
- The variable
linescontains a list of strings, each ending with\nto create a new line. - The
open()function opensexample2.txtin write mode ('w'). - The
writelines()method writes all lines from the list to the file.
Output:

The file example2.txt contains:

3. Appending a String to a File
The append mode ('a') allows adding content to an existing file without erasing its contents.

main.py
# Define the text to append
append_text = "This is an additional line.\n"
# Open the file in append mode
with open("example3.txt", "a") as file:
file.write(append_text)
print("String appended to file successfully.")
Explanation:
- The variable
append_textcontains the string to be added. - The file
example3.txtis opened in append mode ('a'). - The
write()method adds the string at the end of the existing content.
Output:

If example3.txt had existing content, the new line is added without overwriting it.

4. Writing a String Using print() with File Parameter
The print() function can write to a file by specifying the file parameter.
# Define the string
message = "Writing to file using print()."
# Open file in write mode and use print()
with open("example4.txt", "w") as file:
print(message, file=file)
print("String written using print() function.")
Explanation:
- The variable
messagecontains the string. - The file
example4.txtis opened in write mode. - The
print()function writes the string to the file using thefileparameter.
Output:

The file example4.txt contains:

Conclusion
In this tutorial, we covered different ways to write a string to a file in Python:
write(): Writes a single string.writelines(): Writes multiple strings.- Appending Mode (
'a'): Adds content without overwriting. print()with File Parameter: Uses print function for writing.
