How to Write a String to a Text File using Python

To write a string to a text file using Python:

text_file = open(r"path where the text file will be created\file name.txt", "w")
my_string = "type your string here"
text_file.write(my_string)
text_file.close()

In this short guide, you’ll see how to:

  • Write a string to a text file
  • Overwrite the original string
  • Display a list of strings in the text file using a For Loop
  • Deal with integers

Steps to Write a String to a Text File using Python

Step 1: Specify the path for the text file

To begin, specify the path where the text file will be created on your computer.

For example, assume that a text file (called “Example” with “.txt” file type) will be created under the following path:

C:\Users\Ron\Desktop\Test\Example.txt

Step 2: Write a string to a text file using Python

Next, write your string to the text file using this template:

text_file = open(r"path where the text file will be created\file name.txt", "w")
my_string = "type your string here"
text_file.write(my_string)
text_file.close()

For our example:

  • The path where the text file will be created is: C:\Users\Ron\Desktop\Test\Example.txt
  • my_string contains the following text: “This is a Test

So the full Python code would be (don’t forget to place “r” before your path name to avoid errors in the path):

text_file = open(r"C:\Users\Ron\Desktop\Test\Example.txt", "w")
my_string = "This is a Test"
text_file.write(my_string)
text_file.close()

Once you run the code (adjusted to your path), you’ll then see the new text file in your specified location.

If you open the text file, you’ll see the actual string:

This is a Test

Overwrite the String

What if you want to overwrite the original string with a new value?

For instance, what if you want to change the string to the following value:

This is a NEW Test

In that case, simply edit the text as follows:

my_string = "This is a NEW Test"

So the new Python code would look like this:

text_file = open(r"C:\Users\Ron\Desktop\Test\Example.txt", "w")
my_string = "This is a NEW Test"
text_file.write(my_string)
text_file.close()

Run the code, and you’ll see the new string:

This is a NEW Test

List of Strings

Say that you want to display a list of strings in your text file.

Here is an example of a list with strings:

my_list = ["This is a Test", "This is ALSO a Test", "This is a FINAL Test"]

In that case, you may use a For Loop to display your list of strings in the text file:

text_file = open(r"C:\Users\Ron\Desktop\Test\Example.txt", "w")
my_list = ["This is a Test", "This is ALSO a Test", "This is a FINAL Test"]

for i in my_list:
text_file.write(i + "\n")

text_file.close()

You’ll now see that each of the strings is presented in a new line:

This is a Test
This is ALSO a Test
This is a FINAL Test

Dealing with Integers

If you try to write integers into the text file, you’ll get the following error:

write() argument must be str, not int

You may then choose to convert the integers to strings.

For example, suppose that you have two integers (3 and 5), and you want to present the sum of those integers in the text file.

In that case, apply the following syntax to achieve the above goal (notice that str() was used to convert the integers to strings):

text_file = open(r"C:\Users\Ron\Desktop\Test\Example.txt", "w")
sum_values = 3 + 5
text_file.write(str(sum_values))
text_file.close()

You’ll then get the sum of 8 in the text file:

8

Leave a Comment