Python StringIO

Under various circumstances, data reading and writing do not have to be done in files but must be done in memory. Python provides a built-in module named “StringIO”, which comes in handy here. This could generate file-like objects that can be read and written in a similar way that files can. This object can perform the bulk of the operations that consumers assume from a standard file object.

When you construct a “StringIO” object, you can pass the string to the constructor whenever you want to initialize it to an already existing string value. When no string is provided, StringIO would begin with an empty string. For both cases, the displayed pointer upon that file will begin at zero. StringIO can handle Unicode or even 8-bit strings. Although, conflating both may require some caution.

However, the most recent Python edition does not include this package. Therefore, we must import this module as io.StringIO using the Python io module to use it. It supports different forms of I/O, including textual, binary, as well as raw data. Objects of these categories are referred to as file objects or streams.

Installing The StringIO Module in Python:

As we have discussed above, to use the StringIO module, we must have to import it from the Python provided io library.

from io import StringIO

We will be utilizing this for the implementation of example codes in this article.

StringIO Object for Reading a File:

Here, we will perform a practical example to let you understand how to read a file in StringIO. The example code is given below:

from io import StringIO
mystring= "This is the practical demonstration of example code"
myfile= StringIO(mystring)

We will begin with a simple string. We have named the string “mystring” which is storing an alphanumeric value. Now, to set it as a file object we will make use of the “StringIO” function, which will make it an object file. As the “StringIO” function has already been applied, we can now utilize this just as we do with a file.

The “myfile” object will store this file. We will exercise the “.read()” function to read this file. Write the name of the object holding the file with the “.read” function. As in our example, it is “”. This will read the data stored inside the particular object file. Then, will display the output via “print()” statement.

The output generated from the above-mentioned code is:

StringIO Object for Writing into a File:

Just like reading the StringIO object, we can also write on the StringIO object. For this, we will be using the “write()” function to write data.

from io import StringIO
mystring= “Happy python Learning.”
myfile= StringIO(mystring)
myfile.write("You are using StringIO object in python.")
print("Our string will appear like this:",

In the above code snippet, we have created a string and by using the “StringIO” we set it to an object file as we have explained in the above paragraph. Now, for writing the data to the “StringIO” object, the python function we have employed is “write()”. A given text is written to a file using the “write()” function. But before that, we have to add the name of the file on which we need to write data.

In the example code you can see “myfile.write()” inside the braces of the “write()” function we have defined the text to be written on the object file. The other we have used here is “seek()”. This method is employed to alter the position of the cursor to a specified position. Setting the value to “0” refers to the start position of the specified file. Inside the “print()” statement, we have defined a text string followed by a “read()” function.

The resultant string can be seen in the image below.

Using The StringIO Method “getvalue()”:

Irrespective of the present location, the “get value()” function simply returns the entire data of that object file.

from io import StringIO
newstring= "You are learning the use of StringIo object in python. Happy learning."
newfile= StringIO(newstring)

This chunk of code uses the “getvalue()” method of the StringIO. We have created a string with the name “newstring” and assigned it a string value. Then, we generated an object file “newfile” to store this string. The “getvalue()” function is called with the defined file name, inside the parentheses of “print()” statement. It will fetch whatsoever the data stored inside the object file.

The output string is displayed below.

Using The StringIO Method “truncate()”:

The next technique we are going to talk about is the “truncate()” function of StringIO. Python’s “truncate()” method resizes a file to a predetermined size. This function saves the file when it has been dropped after the specified index.

newstring= "You are learning the use of StringIo object in python."
newfile= StringIO(newstring)

Inside the “truncate()” function we have passed an integer value which is “25”. It is referring to the index from where the file content will be discarded from. In our example, the data in the object file is displayed up to the index we have defined in the code and the rest of the data is excluded.

The output screen is presented here.

Using The StringIO Function “close()”:

The last method we are elaborating is the “close()” method of StringIO. Executing it would prohibit any actions from being performed on the object file.


The “close()” function here will close the specified object file. So, no operation can be applied to the file. Any subsequent operations would lead to a ValueError. The outcome is displayed in the below image.


In this article, we have reviewed the StringIO function in Python. We explained the StringIO method and what it is used for. Furthermore, with the implementation of practical example codes in the Spyder tool we have elaborated different methods of StringIO. A thorough and mindful study of this article will help you learn the concept of StringIO in python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content