How to Write a String to a Text File in Python?

In this artcile, we will discuss how to write a string to a text file in Python.

Table Of Contents

Introduction

To write any content into a file, first, we have to open a file. The open() is used to open a file. Once a file is open, then using its object, we can write text into that file, using the write() method.

Write a string to a text file in write mode (w)

We can write a string into a file with write mode. For this, we have to specify w as write mode while opening the file.

Syntax:

with open ('file_name.txt', 'w') as file:  
    file.write(input_str)

Here, input_str is the string that is written to a file ‘file_name.txt’ and w specifies write mode.

Example:

In this example, we will write a string – ‘welcome to thisPointer’ into an empty file named – thispointer.txt.

# Consider the input string
input_str = 'welcome to thisPointer'

# Open the file named - thisPointer.txt
with open ('thisPointer.txt', 'w') as file:  
    # write the string into the file 
    file.write(input_str)  

It will create a file ‘thisPointer.txt’ with the following contents,

welcome to thisPointer

Write a string to a text file in write & read mode (w+)

We can write a string into a file with write and read mode. For this, we have to specify w+ as write & read mode.

Syntax:

with open ('file_name.txt', 'w+') as file:  
    file.write(input_str)

Here, input_str is the string that is written to a file, and w+ specifies write and read mode,

Example:
In this example, we will write a string – ‘welcome to thisPointer’ into an empty file named – thispointer.txt file.

# Consider the input string
input_str = 'welcome to thisPointer'

# Open the file named - thisPointer.txt
with open ('thisPointer.txt', 'w+') as file:  
    # write the string into the file 
    file.write(input_str)  

It will create a file ‘thisPointer.txt’ with the following contents,

welcome to thisPointer

Write a string to a text file in append mode (a)

We can write a string into a file with append mode. For this, we have to specify a as append mode. It will add the new string to the end of file. If file has existing content, then it will remain there and new text will be added in the end.

Syntax:

with open ('file_name.txt', 'a') as file:  
    file.write(input_str)

Here, input_str is the string that is written to a file and a specifies append mode,

Example:

In this example, we will write a string – ‘welcome to thisPointer’ into a file named – thispointer_data1.txt file that has already existing content – “I am already in file!!”

# Consider the input string
input_str = 'welcome to thisPointer'

# Open the file named - thisPointer.txt
with open ('thispointer_data1.txt', 'a') as file:  
    #append the string into the file 
    file.write(input_str) 

It will add the text at the end of file. The contents of file will be,

I am already in file!!
welcome to thisPointer

In this example text got appended to the end of file and existing content remains the same.

Write a string to a text file in append & read mode (a+)

We can write a string into a file with append and read mode. For this, we have to specify a+ as append & read mode. So We will add the new string to the file that has existing content.

Example:

In this example, we will write a string – ‘welcome to thisPointer’ into a file named – thispointer_data1.txt file that has already existing content – “I am already in file!!”

# Consider the input string
input_str = 'welcome to thisPointer'

# Open the file named - thisPointer.txt
with open ('thisPointer_data1.txt', 'a+') as file:  
    #append the string into the file 
    file.write(input_str)  

Output:
Now the file is downloaded. Let’s open it.

It will add the text at the end of file. The contents of file will be,

I am already in file!!
welcome to thisPointer

New string got appended to the file and existing content in the file remained same.

Write a list of strings to a file using writelines()

It is possible to write multiple strings at a time into the file using writelines() method.

Syntax:

with open ('file_name.txt', 'a') as file:  
    file.writelines(listOfStrings) 

Here, listOfStrings is a list of strings. This code will write / append all the strings in the list to the file.

Example:

In this example, we will write three strings at the end of file -thisPointer3.txt .

# Consider the input strings ina list
listOfStrings =[ 'welcome to thisPointer','python','cpp']

# Open the file named - thisPointer.txt
with open ('thisPointer3.txt', 'a') as file:  
    #append the string into the file 
    file.writelines(listOfStrings)   

Output:

welcome to thisPointerpythoncpp

You can see that strings are written to a file. If file has some existing content, then it will not replace the content. It will just add the new text at the end of file.

Summary

In this Python string tutorial, we have seen how to write a string to a text file using write and append modes. It is also possible to write multiple strings at a time through a list using the writelines() method.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top