Python UserString
Published Oct 24, 2025
Contribute to Docs
In Python, a UserString is a class in the collections module. It is a custom wrapper for string objects, behaving like a string but allowing easier subclassing. Unlike directly subclassing str, UserString stores its content in the .data attribute.
Note: While
UserStringbehaves like a string and supports the same operations, some methods return a regularstrinstead of anotherUserString.
Syntax
myString = collections.UserString(seq)
seq: It can be anything that can be converted into astroriterable of characters, like a list or tuple of individual characters.
Return value:
- A
UserStringinstance, which is an object that contains the content derived fromseq.
Example
The following example demonstrates the usage of the UserString method:
from collections import UserStringmyString = 'First example of a UserString!'customString = UserString(myString)print(customString.data)
This is the output of the above code:
First example of a UserString!
Codebyte Example
The following example creates a UserString and demonstrates its usage more in detail, along with a subclass that removes vowels:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours