How to Write String to a Text File in Python?


A string is a primitive data structure that stores a sequence/collection of characters, alphabets, or words. In Python, users can manipulate strings using a variety of built-in methods and operations. For instance, reading a text file via the read() function, writing to a text file using the write() function, and so on. This post will guide you on how to write a string or multiple strings to a text file in python.

How to Write String to a Text File in Python?

In Python, use an in-built open() function with the ‘w’ mode to write a string(s) to a text file. This function retrieves a file object, which can be used to write to the file. Syntax:
Open('fileName', 'w');
Let’s put it into practice!

Example: Python Program to Write a Single String to a Text File

The steps for writing a single string to a text file in Python are shown below: Step 1: Use the below code to open a text file using python.
with open('exampleFile.txt', 'w') as f:
In this example, ‘exampleFile.txt’ is a text file to which you want to write, and ‘w’ specifies that the file should be opened in write mode. The “with” statement is used to ensure that the file is closed properly after writing. Alternatively, you can close a file with the close() function. Step 2: Use the write() function to write a string of your choice to the text file.
f.write('Welcome to linuxgenie.net')

Complete Code Snippet:

Output: Now open the text file to see the resultant output.
Note: The text file you are writing to will be created if it does not exist; however, If it already exists, then the content of that file will be overwritten.

Example: Python Program to Write Multiple Strings to a Text File

In Python, the writelines() function is used to write more than one string(in the form of a list) to a text file.
f.writelines(['Welcome\n', 'to\n', 'linuxgenie.net\n'])

Complete Code Snippet:

Output:

How to Append String to a Text File in Python?

When you use the ‘w’ mode within the open() function, the content specified in the open() function will overwrite the content of the already existing targeted file. However, if you want to append the strings instead of overwriting, then use the ‘a’ mode within the open() function. Syntax:
Open('fileName', 'a');
Let’s put it into practice!

Example: Python Program to Append a Single String to a Text File

Here’s an example code for appending a string to a text file in python: Step 1: Open the “exampleFile” in appending mode, as follows:
open('exampleFile.txt', 'a') as f:
Step 2: The following line of code will append a  “Programming World” string to the “exampleFile.txt”:
f.write('Programming World')
Step 3: Close the File.
f.close()

Complete Code Snippet:

Output:

A string is appended at the end of the text file.

Conclusion

In Python, you can write a string to a text file by opening it in appending or writing mode. After that, use the write() function or writelines() function to write a single or multiple strings of your choice into the selected file, and then close it with the close() function or by using the “with” statement.
Print Friendly, PDF & Email