{"id":25821,"date":"2021-12-16T09:19:14","date_gmt":"2021-12-16T03:49:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=25821"},"modified":"2021-12-16T09:19:14","modified_gmt":"2021-12-16T03:49:14","slug":"python-program-for-collections-userstring-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-collections-userstring-function\/","title":{"rendered":"Python Program for collections.UserString Function"},"content":{"rendered":"
Python collections.UserString() Function:<\/strong><\/p>\n 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.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\ncollections.UserString(sequence)<\/pre>\n
Program for collections.UserString Function in Python<\/h2>\n
\n
Method #1: Using collections Module (Static Input)<\/h3>\n
\n
# Import UserString() function from the collections module using the import keyword.\r\nfrom collections import UserString\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = 'hello'\r\n# Pass the given string as an argument to the UserString() function to\r\n# create a user string for the given string.\r\n# Store it in another variable.\r\nrsltusr_str = UserString(gvn_str)\r\n# Print the above result.\r\nprint(\"The user string for the given string is :\")\r\nprint(rsltusr_str.data)\r\n# Create an empty user string using the UserString() function\r\n# and store it in another variable.\r\nprint(\"The empty user string is :\")\r\nemty_rsltstr = UserString(\"\")\r\n# Print the above obtained empty user string.\r\nprint(emty_rsltstr.data)\r\n<\/pre>\n
The user string for the given string is :\r\nhello\r\nThe empty user string is :\r\n<\/pre>\n
\n
# Import UserString() function from the collections module using the import keyword.\r\nfrom collections import UserString\r\n\r\n\r\n# Create a class by passing the UserString function as an argument.\r\nclass Given_String(UserString):\r\n\r\n # Inside the class, create a function by passing the second string as an argument\r\n # for appending some random string\r\n def append(self, str2):\r\n # Concatenate the second string to the given string using the string concatenation.\r\n self.data += str2\r\n\r\n # Create another function by passing the second string as an argument\r\n # for removing some random string from the given string\r\n def remove(self, str2):\r\n # Inside the function, remove all the second-string characters by replacing it with\r\n # null string using the replace() function.\r\n self.data = self.data.replace(str2, \"\")\r\n\r\n\r\n# In the main Function\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"Python\"\r\n# Create an object for the above class by passing the given string as an argument\r\n# and store it in another variable.\r\nrsltstr = Given_String(gvn_str)\r\n# Print the string before appending.\r\nprint(\"The string before appending is :\", rsltstr.data)\r\n\r\n# Append some random string to the above obtained result string using the append() function\r\nrsltstr.append(\"programs\")\r\n# Print the string after appending.\r\nprint(\"After appending the Result string is :\", rsltstr.data)\r\n\r\n# Remove some random string from the above obtained result string using the remove() function\r\nrsltstr.remove(\"rams\")\r\n# Print the string after removing.\r\nprint(\"After Removing the Result string is :\", rsltstr.data)\r\n<\/pre>\n
The string before appending is : Python\r\nAfter appending the Result string is : Pythonprograms\r\nAfter Removing the Result string is : Pythonprog<\/pre>\n
Method #2: Using collections Module (User Input)<\/h3>\n
\n
# Import UserString() function from the collections module using the import keyword.\r\nfrom collections import UserString\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Pass the given string as an argument to the UserString() function to\r\n# create a user string for the given string.\r\n# Store it in another variable.\r\nrsltusr_str = UserString(gvn_str)\r\n# Print the above result.\r\nprint(\"The user string for the given string is :\")\r\nprint(rsltusr_str.data)\r\n# Create an empty user string using the UserString() function\r\n# and store it in another variable.\r\nprint(\"The empty user string is :\")\r\nemty_rsltstr = UserString(\"\")\r\n# Print the above obtained empty user string.\r\nprint(emty_rsltstr.data)\r\n<\/pre>\n
Enter some random string = 3436hello\r\nThe user string for the given string is :\r\n3436hello\r\nThe empty user string is :<\/pre>\n
\n
# Import UserString() function from the collections module using the import keyword.\r\nfrom collections import UserString\r\n\r\n\r\n# Create a class by passing the UserString function as an argument.\r\nclass Given_String(UserString):\r\n\r\n # Inside the class, create a function by passing the second string as an argument\r\n # for appending some random string\r\n def append(self, str2):\r\n # Concatenate the second string to the given string using the string concatenation.\r\n self.data += str2\r\n\r\n # Create another function by passing the second string as an argument\r\n # for removing some random string from the given string\r\n def remove(self, str2):\r\n # Inside the function, remove all the second-string characters by replacing it with\r\n # null string using the replace() function.\r\n self.data = self.data.replace(str2, \"\")\r\n\r\n\r\n# In the main Function\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Create an object for the above class by passing the given string as an argument\r\n# and store it in another variable.\r\nrsltstr = Given_String(gvn_str)\r\n# Print the string before appending.\r\nprint(\"The string before appending is :\", rsltstr.data)\r\n\r\n# Append some random string to the above obtained result string using the append() function\r\nrsltstr.append(\"morning all\")\r\n# Print the string after appending.\r\nprint(\"After appending the Result string is :\", rsltstr.data)\r\n\r\n# Remove some random string from the above obtained result string using the remove() function\r\nrsltstr.remove(\"all\")\r\n# Print the string after removing.\r\nprint(\"After Removing the Result string is :\", rsltstr.data)\r\n<\/pre>\n
Enter some random string = good\r\nThe string before appending is : good\r\nAfter appending the Result string is : goodmorning all\r\nAfter Removing the Result string is : goodmorning<\/pre>\n","protected":false},"excerpt":{"rendered":"