Collections UserString in Python

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.
Example:

Python3

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


Output:

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

String with the use of Double Quotes: 
I love coding.

Note: For more information, refer to Python String

Collections.UserString

Python supports a String simillar as a container called UserString present in the collections module. This class acts when 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 when 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 property of this class.
Syntax:

collections.UserString(seq)

Example 1:

Python3

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


Output:

12344

Example 2:

Python3

# 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):
        self.data += s
        
    # Function to remove from
    # string
    def remove(self, s):
        self.data = self.data.replace(s, "")
    
# Driver's code
s1 = Mystring("Codes")
print("Original String:", s1.data)
# Appending to string
s1.append("s")
print("String After Appending:", s1.data)
# Removing from string
s1.remove("e")
print("String after Removing:", s1.data)


Output:

Original String: Codes
String After Appending: Codess
String after Removing: Codss

Explore More

Python 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the