StringIO Module in Python
Last Updated :
12 Jun, 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 disk I/O. To use StringIO, you need to import it from the io module:
from io import StringIO
Example:
Python
from io import StringIO
s = 'This is initial string.'
f = StringIO(s) # StringIO object
print(f.read())
f.write(" Welcome to GeeksForGeeks.")
f.seek(0)
print(f.read())
OutputThis is initial string.
This is initial string. Welcome to GeeksForGeeks.
Explanation:
- f.read() reads from the current cursor to the end, moving the cursor to the end after reading.
- f.write(" Welcome to GeeksForGeeks.") appends the new string at the current cursor position (end of the stream).
- f.seek(0) moves the cursor back to the beginning of the stream.
- f.read() reads the full content from the start, printing the combined text.
StringIO methods and functions
1. getvalue(): This method returns the entire contents of the StringIO object as a single string. It is helpful when you want to retrieve all the text you’ve written to the stream.
Python
from io import StringIO
f = StringIO("Hello and welcome to GeeksForGeeks.")
print(f.getvalue())
OutputHello and welcome to GeeksForGeeks.
Explanation: This code creates a StringIO text stream initialized with a string and prints its entire content using getvalue().
2. Boolean utility functions: These are functions that returns either True or False. They are used to check specific properties or conditions of an object like readability, writability, etc. Let’s understand the following boolean utility functions with the help of the table below:
Function | Description | Returns |
---|
isatty() | Checks if the stream is interactive | False (always) |
---|
readable() | Checks if the stream can be read | True |
---|
writable() | Checks if the stream supports writing | True |
---|
seekable() | Checks if the stream allows moving the cursor | True |
---|
closed | Checks if the stream is closed | True or False |
---|
Python
from io import StringIO
f = StringIO("Sample")
print(f.isatty())
print(f.readable())
print(f.writable())
print(f.seekable())
print(f.closed)
OutputFalse
True
True
True
False
Explanation: This code creates a StringIO text stream and uses boolean functions to check and print whether the stream is interactive, readable, writable, seekable or closed.
3. seek(position): This method allows you to move the internal cursor to a specific position within the stream. It is necessary if you want to read or write from a particular point.
Python
from io import StringIO
f = StringIO("Hello")
print(f.read())
print(f.read())
f.seek(0)
print(f.read())
Explanation: This code creates a StringIO stream with "Hello", reads and prints it. The second read prints nothing as the cursor is at the end. After seek(0), it reads and prints the content again.
4. truncate(size=None): This method resizes the stream. If a size is provided, the stream is cut off after that many characters. If no size is provided, it truncates the stream at the current cursor position.
Python
from io import StringIO
f = StringIO("Hello and welcome")
f.seek(0)
f.truncate(10)
f.seek(0)
print(f.read())
Explanation: This code creates a StringIO stream, truncates its content to 10 characters, resets the cursor and prints the truncated content.
5. tell(): This method returns the current position of the cursor in the stream. It helps you keep track of where you are reading or writing.
Python
from io import StringIO
f = StringIO("Python")
print(f.tell())
f.read(3)
print(f.tell())
Explanation: This code creates a StringIO stream with "Python" and prints the initial cursor position (0). It then reads 3 characters, moves the cursor and prints the new position (3).
6. close(): This method closes the stream. Once the stream is closed, you can no longer read from or write to it and attempting to do so will raise an error.
Python
from io import StringIO
f = StringIO("Python")
f.close()
print(f.closed)
Explanation: This code creates a StringIO stream with "Python", closes the stream and then prints whether the stream is closed .
Similar Reads
Python String Module The string module is a part of Python's standard library and provides several helpful utilities for working with strings. From predefined sets of characters (such as ASCII letters, digits and punctuation) to useful functions for string formatting and manipulation, the string module streamlines vario
4 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Python String - printable() In Python string.printable is a pre-initialized string constant that contains all characters that are considered printable. This includes digits, ASCII letters, punctuation, and whitespace characters.Let's understand with an example:Pythonimport string # to show the contents of string.printable prin
2 min read
Python String Methods Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio
5 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python Typer Module Typer is a library for building powerful command-line interface applications in the easiest way. It is easier to read and the simplest way to create a command line application rather than using the standard Python library argparse, which is complicated to use. It is based on Python 3.6+ type hints a
5 min read
Python sys Module The sys module in Python provides access to variables and functions that interact closely with the Python interpreter and runtime environment. It allows developers to manipulate various aspects of program execution and the interpreter itself. It's Key capabilities include:Accessing command-line argu
4 min read
Python string length The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method.Example:Pythons1 = "abcd" print(len(s1)) s2 = "" print(len(s2)) s3 = "a" print(len(s3))Output4 0 1 String len() Syntaxlen(string) ParameterString:
4 min read
How to Install a Python Module? A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read
Python String A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1.Pythons = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # update print(s1) # printOut
6 min read