Python collections.UserString() Function:
Python’s collections module includes a String-like container called UserString. This class serves as a wrapper class for string objects. This class is useful when one wants to create their own string with modified or new functionality. It can be thought of as a method of adding new behaviors to the string. This class takes any string-convertible argument and simulates a string whose content is kept in a regular string. This class’s data attribute provides access to the string.
Syntax:
collections.UserString(sequence)
Program for collections.UserString Function in Python
Method #1: Using collections Module (Static Input)
Approach:
- Import UserString() function from the collections module using the import keyword.
- Give the string as static input and store it in a variable.
- Pass the given string as an argument to the UserString() function to create a user string for the given string.
- Store it in another variable.
- Print the above result.
- Create an empty user string using the UserString() function and store it in another variable.
- Print the above obtained empty user string.
- The Exit of the Program.
Below is the implementation:
# Import UserString() function from the collections module using the import keyword.
from collections import UserString
# Give the string as static input and store it in a variable.
gvn_str = 'hello'
# Pass the given string as an argument to the UserString() function to
# create a user string for the given string.
# Store it in another variable.
rsltusr_str = UserString(gvn_str)
# Print the above result.
print("The user string for the given string is :")
print(rsltusr_str.data)
# Create an empty user string using the UserString() function
# and store it in another variable.
print("The empty user string is :")
emty_rsltstr = UserString("")
# Print the above obtained empty user string.
print(emty_rsltstr.data)
Output:
The user string for the given string is : hello The empty user string is :
Example2:
Approach:
- Import UserString() function from the collections module using the import keyword.
- Create a class by passing the UserString function as an argument.
- Inside the class, create a function by passing the second string as an argument for appending some random string.
- Concatenate the second string to the given string using the string concatenation.
- Create another function by passing the second string as an argument for removing some random string from the given string.
- Inside the function, remove all the second-string characters by replacing it with a null string using the replace() function.
- In the main Function,
- Give the string as static input and store it in a variable.
- Create an object for the above class by passing the given string as an argument and store it in another variable.
- Print the string before appending.
- Append some random string to the above-obtained result string using the append() function.
- Print the string after appending.
- Remove some random string from the above-obtained result string using the remove() function.
- Print the string after removing.
- The Exit of the Program.
Below is the implementation:
# Import UserString() function from the collections module using the import keyword.
from collections import UserString
# Create a class by passing the UserString function as an argument.
class Given_String(UserString):
# Inside the class, create a function by passing the second string as an argument
# for appending some random string
def append(self, str2):
# Concatenate the second string to the given string using the string concatenation.
self.data += str2
# Create another function by passing the second string as an argument
# for removing some random string from the given string
def remove(self, str2):
# Inside the function, remove all the second-string characters by replacing it with
# null string using the replace() function.
self.data = self.data.replace(str2, "")
# In the main Function
# Give the string as static input and store it in a variable.
gvn_str = "Python"
# Create an object for the above class by passing the given string as an argument
# and store it in another variable.
rsltstr = Given_String(gvn_str)
# Print the string before appending.
print("The string before appending is :", rsltstr.data)
# Append some random string to the above obtained result string using the append() function
rsltstr.append("programs")
# Print the string after appending.
print("After appending the Result string is :", rsltstr.data)
# Remove some random string from the above obtained result string using the remove() function
rsltstr.remove("rams")
# Print the string after removing.
print("After Removing the Result string is :", rsltstr.data)
Output:
The string before appending is : Python After appending the Result string is : Pythonprograms After Removing the Result string is : Pythonprog
Method #2: Using collections Module (User Input)
Approach:
- Import UserString() function from the collections module using the import keyword.
- Give the string as user input using the input() function and store it in a variable.
- Pass the given string as an argument to the UserString() function to create a user string for the given string.
- Store it in another variable.
- Print the above result.
- Create an empty user string using the UserString() function and store it in another variable.
- Print the above obtained empty user string.
- The Exit of the Program.
Below is the implementation:
# Import UserString() function from the collections module using the import keyword.
from collections import UserString
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Pass the given string as an argument to the UserString() function to
# create a user string for the given string.
# Store it in another variable.
rsltusr_str = UserString(gvn_str)
# Print the above result.
print("The user string for the given string is :")
print(rsltusr_str.data)
# Create an empty user string using the UserString() function
# and store it in another variable.
print("The empty user string is :")
emty_rsltstr = UserString("")
# Print the above obtained empty user string.
print(emty_rsltstr.data)
Output:
Enter some random string = 3436hello The user string for the given string is : 3436hello The empty user string is :
Example2:
Approach:
- Import UserString() function from the collections module using the import keyword.
- Create a class by passing the UserString function as an argument.
- Inside the class, create a function by passing the second string as an argument for appending some random string.
- Concatenate the second string to the given string using the string concatenation.
- Create another function by passing the second string as an argument for removing some random string from the given string.
- Inside the function, remove all the second-string characters by replacing it with a null string using the replace() function.
- In the main Function,
- Give the string as user input using the input() function and store it in a variable.
- Create an object for the above class by passing the given string as an argument and store it in another variable.
- Print the string before appending.
- Append some random string to the above-obtained result string using the append() function.
- Print the string after appending.
- Remove some random string from the above-obtained result string using the remove() function.
- Print the string after removing.
- The Exit of the Program.
Below is the implementation:
# Import UserString() function from the collections module using the import keyword.
from collections import UserString
# Create a class by passing the UserString function as an argument.
class Given_String(UserString):
# Inside the class, create a function by passing the second string as an argument
# for appending some random string
def append(self, str2):
# Concatenate the second string to the given string using the string concatenation.
self.data += str2
# Create another function by passing the second string as an argument
# for removing some random string from the given string
def remove(self, str2):
# Inside the function, remove all the second-string characters by replacing it with
# null string using the replace() function.
self.data = self.data.replace(str2, "")
# In the main Function
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Create an object for the above class by passing the given string as an argument
# and store it in another variable.
rsltstr = Given_String(gvn_str)
# Print the string before appending.
print("The string before appending is :", rsltstr.data)
# Append some random string to the above obtained result string using the append() function
rsltstr.append("morning all")
# Print the string after appending.
print("After appending the Result string is :", rsltstr.data)
# Remove some random string from the above obtained result string using the remove() function
rsltstr.remove("all")
# Print the string after removing.
print("After Removing the Result string is :", rsltstr.data)
Output:
Enter some random string = good The string before appending is : good After appending the Result string is : goodmorning all After Removing the Result string is : goodmorning