It's used when you have some API that only takes files, but you need to use a string. For example, to compress a string using the gzip module in Python 2:

import gzip
import StringIO

stringio = StringIO.StringIO()
gzip_file = gzip.GzipFile(fileobj=stringio, mode='w')
gzip_file.write('Hello World')
gzip_file.close()

stringio.getvalue()
Answer from Petr Viktorin on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › stringio-module-in-python
StringIO Module in Python - GeeksforGeeks
July 12, 2025 - StringIO is a module in Python that allows you to treat strings as file-like objects. It provides an in-memory stream for text I/O (input/output), which means you can read from and write to a string just like you would with a file, but without ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-io-bytesio-stringio
Python io.BytesIO and io.StringIO: Memory File Guide | DigitalOcean
August 3, 2022 - import io data = io.StringIO() data.write('JournalDev: ') print('Python.', file=data) print(data.getvalue()) data.close()
🌐
AskPython
askpython.com › python › string › stringio-in-python3
How To Use StringIO In Python3? - AskPython
May 31, 2023 - The different sections related ... is present in the form of a file object, then this StringIO class helps to handle the basic operation over string files....
🌐
Readthedocs
pydoc-zh.readthedocs.io › en › latest › library › stringio.html
7.5. StringIO — Read and write strings as files — Python 2.7.6 documentation
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects).
🌐
TutorialsPoint
tutorialspoint.com › article › complete-guide-to-python-stringio-module-with-examples
Complete Guide to Python StringIO Module with Examples
August 21, 2023 - The Python StringIO module provides an in-memory file-like object that allows you to work with strings as if they were files. This eliminates the need to create temporary files on disk, making operations faster and more memory-efficient.
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › io.StringIO.html
io.StringIO — Python Standard Library
io.StringIO · View page source · class io.StringIO¶ · Text I/O implementation using an in-memory buffer. The initial_value argument sets the value of object.
Find elsewhere
🌐
GitHub
github.com › python › cpython › blob › main › Modules › _io › stringio.c
cpython/Modules/_io/stringio.c at main · python/cpython
#include "Python.h" #include <stddef.h> // offsetof() #include "pycore_object.h" #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include "_iomodule.h" · /* Implementation note: the buffer is always at least one character longer · than the enclosed string, for proper functioning of _PyIO_find_line_ending. */ · #define STATE_REALIZED 1 · #define STATE_ACCUMULATING 2 · · /*[clinic input] module _io · class _io.StringIO "stringio *" "clinic_state()->PyStringIO_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2693eada0658d470]*/ ·
Author   python
🌐
Webkul
webkul.com › home › python: using stringio and bytesio for managing data as file object
Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog
October 1, 2019 - ... StringIO.StringIO allows either Unicode or Bytes string. cStringIO.StringIO requires a string that is encoded as a bytes string. ... >>> import io >>> string_out = io.StringIO() >>> string_out.write('A sample string which we have to send ...
🌐
Python Pool
pythonpool.com › home › blog › python stringio module from scratch with examples
Python StringIO Module from Scratch with Examples - Python Pool
March 8, 2022 - This is where the Python StringIO module comes into play. ... StringIO extends an abstract base class known as TextIOBase. It is a class that handles streams whose bytes are represented in text format.
🌐
Readthedocs
ironpython-test.readthedocs.io › en › latest › library › stringio.html
7.5. StringIO — Read and write strings as files — IronPython 2.7.2b1 documentation
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects).
🌐
Real Python
realpython.com › videos › using-stringio-simulate-file
Using io.StringIO to Simulate a File (Video) – Real Python
So a way that you can simulate such a file object is by importing StringIO from the io module. So we’ll say from io import StringIO. And StringIO is a file-like object that lives inside your memory. So I can create this, let’s call it fake_file ...
Published   July 30, 2024
🌐
Centron
centron.de › startseite › python io – bytesio and stringio
Python io - BytesIO and StringIO
February 7, 2025 - Here is a sample program to demonstrate this: ... The getvalue() function just takes the value from the Buffer as a String. We can even use StringIO as well which is extremely similar in use to BytesIO.
Top answer
1 of 2
32

http://docs.python.org/library/io.html#io.StringIO

http://docs.python.org/library/stringio.html

I see this.

An in-memory stream for unicode text. It inherits TextIOWrapper.

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).

io.StringIO is a class. It handles Unicode. It reflects the preferred Python 3 library structure.

StringIO.StringIO is a class. It handles strings. It reflects the legacy Python 2 library structure.

What should be preferred?

Always move forward toward the new library organization. The io.open should be used to replace the built-in Unicode-unaware open.

Forward. Move forward.

2 of 2
6

In terms of python 2.7 and 3:

io.BytesIO is an in-memory file-like object that doesn't do any alteration to newlines, and is similar to open(filename, "wb"). It deal with bytes() strings, which in py2.7 is an alias for str.

io.StringIO is an in-memory file-like object that does do alterations to newlines, and is similar to open(filename, "w"). It deal with unicode() strings, which in py3.x is an alias for str.

py2.7's old StringIO.StringIO is an in-memory file-like object that does not do alterations to newlines, and is similar to open(filename, "w"). It deals with both unicode() and bytes() in the same way that most obsolete python 2 string methods do: by allowing you to mix them without error, but only as long as you're lucky.

Thus py2.7's old StringIO.StringIO class is actually more similar to io.BytesIO than io.StringIO, as it is operating in terms of bytes()/str() and doesn't do newline conversions.

What should be preferred?

Don't use StringIO.StringIO, instead use io.BytesIO or io.StringIO, depending on the use-case. This is forward compatible with python 3 and commits to bytes or unicode, rather than "both, maybe".

🌐
Dot Net Perls
dotnetperls.com › stringio-python
Python - StringIO Examples - Dot Net Perls
February 9, 2025 - import io out = io.StringIO() # Print to StringIO stream, no end char. print("Hello", file=out, end="") # Print string contents to console. print(out.getvalue()) ... Is StringIO faster than a series of string appends? I tested a program that wrote large strings, appending strings many times. Result I found StringIO was faster, in two Python implementations.
🌐
Linux Hint
linuxhint.com › python-stringio
Linux Hint – Linux Hint
April 28, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
GitHub
github.com › enthought › Python-2.7.3 › blob › master › Lib › StringIO.py
Python-2.7.3/Lib/StringIO.py at master · enthought/Python-2.7.3
the StringIO will start empty. · The StringIO object can accept either Unicode or 8-bit strings, but · mixing the two may take some care. If both are used, 8-bit strings that · cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause ·
Author   enthought
🌐
LinkedIn
linkedin.com › pulse › python-manage-data-file-object-stringio-sk-rajibul-huda
Python: Manage data as file object (StringIO)
March 22, 2021 - In many of your applications, you're ... temporary file. StringIO handles just this. A StringIO instance is a file-like object that simply appends written data to a location in memory....