How do you write a string to a text file in python?

Contents

  • Introduction
  • Example 1: Write String to Text File
  • Example 2: Write String to Text File in Text Mode
  • Writing other than String to Text File
  • Summary

Now you can save or write string to text a file in persistent data storage using Python.

To write string to a Text File, follow these sequence of steps:

  1. Open file in write mode using open[] function.
  2. Write string to the file using write[] method.
  3. Close the file using close[] method.

Example 1: Write String to Text File

In the following example, we will take a string constant and write the string to a text file by following the above said sequence of steps.

Python Program

text_file = open["sample.txt", "w"]
n = text_file.write['Welcome to pythonexamples.org']
text_file.close[]

Output

29

The write[] method returns the number of characters written to the text file.

Note that this kind of writing to text file, overwrites the data, if the file is already present. If the file is not present, it creates a new file and then writes the string to the file.

Example 2: Write String to Text File in Text Mode

A file can be opened in two modes: the first one is text mode and the second one is binary mode. By default a file is opened in text mode. However, you can explicitly specify the mode.

In the following example, we will open the file in text mode by appending “t” to the mode and write the string to a text file by following the sequence of steps mentioned at the start of this tutorial.

Python Program

text_file = open["sample.txt", "wt"]
n = text_file.write['Welcome to pythonexamples.org']
text_file.close[]

Output

29

Writing other than String to Text File

If you would like to write any Python object other than string or a bytes object to a file, using write[] method, you should first convert that Python object to a string or bytes object.

Summary

In this tutorial of Python Examples, we learned how to write a string to a text file, with the help of example programs.

Python – Write String to Text File

To write string to a file in Python, we can call write[] function on the text file object and pass the string as argument to this write[] function.

In this tutorial, we will learn how to write Python String to a file, with the help of some Python example programs.

Following is the step by step process to write a string to a text file.

  1. Open the text file in write mode using open[] function. The function returns a file object.
  2. Call write[] function on the file object, and pass the string to write[] function as argument.
  3. Once all the writing is done, close the file using close[] function.

Examples

Write String to New Text File

The following is an example Python program in its simplest form to write string to a text file.

Example.py

#open text file
text_file = open["D:/data.txt", "w"]

#write string to file
text_file.write['Python Tutorial by TutorialKart.']

#close file
text_file.close[]

When we run this program, a new file is created named data.txt in D: drive and the string is written to the file. But to programmatically confirm, you can use the value returned by write[] function. write[] function returns the number of bytes written to the file.

Example.py

#open text file
text_file = open["D:/data.txt", "w"]

#write string to file
n = text_file.write['Python Tutorial by TutorialKart.']

#close file
text_file.close[]

print[n]

Output

32

Write to an Existing File

If you try to write a string to an existing file, be careful. When you create a file in write mode and call write[] function, existing data is lost and new data is written to the file.

For instance, in the previous example, we created a file and written some data to it.

Now we shall run the following example.

Example.py

#open text file
text_file = open["D:/data.txt", "w"]

#write string to file
n = text_file.write['Hello World!']

#close file
text_file.close[]

The existing file is overwritten by the new content.

Note: If you would like to append data to a file, open file in append mode and then call write[] function on the file object.

Conclusion

In this Python Tutorial, we learned how to write a string to a text file.

How do I convert a string to a text file in Python?

Python – Write String to Text File Open the text file in write mode using open[] function. The function returns a file object. Call write[] function on the file object, and pass the string to write[] function as argument. Once all the writing is done, close the file using close[] function.

How do you write data to a text file in Python?

To write to a text file in Python, you follow these steps: First, open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. ... Steps for writing to text files..

How do I write a string to a file?

Using writeString[] method of Files class. Using write[] method of Files class. Using writer[] method of Filewriter class..
Create an instance of the file..
Convert the string into a byte array by using string. getBytes[] method..
Lastly call method namely Files. write[] with file instance and the byte array..

How do you write a string in Python?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .

Chủ Đề