Skip to content
Related Articles

Related Articles

Improve Article

Collections.UserString in Python

  • Difficulty Level : Basic
  • Last Updated : 26 Mar, 2020

Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one.


# Python program to demonstrate
# string
# Creating a String  
# with single Quotes 
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: "
# Creating a String 
# with double Quotes 
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: "


String with the use of Single Quotes: 
Welcome to the Geeks World

String with the use of Double Quotes: 
I'm a Geek

Note: For more information, refer to Python String


Python supports a String like a container called UserString present in the collections module. This class acts as a wrapper class around the string objects. This class is useful when one wants to create a string of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the string. This class takes any argument that can be converted to string and simulates a string whose content is kept in a regular string. The string is accessible by the data attribute of this class.



Example 1:

# Python program to demonstrate
# userstring
from collections import UserString
d = 12344
# Creating an UserDict
userS = UserString(d)
# Creating an empty UserDict
userS = UserString("")



Example 2:

# Python program to demonstrate
# userstring
from collections import UserString
# Creating a Mutable String
class Mystring(UserString):
    # Function to append to
    # string
    def append(self, s): += s
    # Function to rmeove from 
    # string
    def remove(self, s): =, "")
# Driver's code
s1 = Mystring("Geeks")
print("Original String:",
# Appending to string
print("String After Appending:",
# Removing from string
print("String after Removing:",


Original String: Geeks
String After Appending: Geekss
String after Removing: Gkss

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :