In this article, our focus point is to write strings to a file in Python. To write a string to a Python file, we first call the write() function on the text file object and then pass the string as a parameter to this write() function. In this quick tutorial, you will learn how to create a text file using a string, replace the original string with the new string, and display a list of strings in the text file using the for loop.
In our first example, we have created an empty file on the desktop. In the code, we have entered the exact location of the text file that we have created. C:\Users\HP\Desktop is the path where the text file is created in our case. After that, we have written the string to the text file through the following code (as you can see in the second screenshot).
The name of the file is my_txt file. The following text is in sample_str=’Welcome to Python’. Below you can find the complete Python code (don’t forget to put a ‘r’ before your path name to avoid any path errors).
sample_str = 'Welcome to Python'
You will see the new text file in your given directory after running the code (tuned to your path). You can view the real string if you open the text file.
In our second example, we are going to add a new value to the original string. Let’s see if we want to alter the string to something like this: ‘This is to overwrite the original text’. In that instance, simply make the following changes to the code. As a result, the updated Python code would be as follows:
sample_str = “This is to overwrite the original text”
As you can see in the following screenshot, the previous text is overwritten with the new text mentioned in the code.
In our last example, we are going to display a list of strings in a file that we have created at the start. It is a string-based list. In that instance, you can display your list of strings in the text file using a for loop. This will print out the list in the file. By adding ‘n’ to the code, each of the strings will now be presented on a new line.
sample_list = ['This is the first line','This is the second line’, 'This is the third line’]
for i in sample_list:
mytxt.write(i + '\n')
As you can see in the following output, that the previous text from the file is removed and a list of strings is displayed as mentioned in the code.
In this article, you have learned that to open a text file for appending, use the open() method with the w or a mode. Always use the close() method to close the file after you’ve finished writing it, or use them with a statement to open it. To write text into a text file, make use of the write() function.
We have explained three examples to help you understand how you can write a text into a file created in whatever location of your system (don’t forget to specify the path accurately), overwrite it, and display a list of strings. Other than strings, you can also deal with integers. The guidelines are almost the same with minor changes when you want to display or calculate integer values.