Collections. Python UserString Unicode characters are represented by strings of bytes. To put it another way, characters are one-character strings. Unfortunately, Python does not recognise this particular data type character. Example:
Production of
# First, we will create a String with a single Quotes
String_1 = 'JavaTpoint is the best platform to learn Python'
print("String with the use of Single Quotes: ")
print(String_1)
# Now, we will create a String with double Quotes
String_2 = "JavaTpoint is the best platform to learn Python"
print("\n String with the use of Double Quotes: ")
print(String_2)
Bytes
String with the use of Single Quotes: JavaTpoint is the best platform to learn Python String with the use of Double Quotes: JavaTpoint is the best platform to learn Python
Collections.UserString
As part of its collection module, Python has its own String container called UserString. This class acts as a wrapper for string objects. With this class, one can easily generate customised or enhanced strings. In order to add additional capabilities to the string, this may be done. Any parameter that can be cast to a string may be used with this class to generate a string representation of an unstructured string. The data property allows you to get to the string. Syntax:
Syntax for UserString
is:
As an example of
collections.UserString(seq)
, consider the following (User dictionary with value and empty)
Input:
from collections import UserString as US
P = 123546
# Here, we will create an UserDict
user_string = US(P)
print("UserString 1: ", user_string.data)
# Now, we will create an empty UserDict
user_string = US("")
print("UserString : ", user_string.data)
Result:
Example 2: A Variable String
UserString 1: 123546 UserString :
(Append function and Remove function)
A Result From The
from collections import UserString as US
# Here, we will create a Mutable String
class User_string(US):
# 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
s_1 = User_string("JavaTpoint")
print("The Original String: ", s_1.data)
# Here, we will Append to string
s_1.append("ing")
print("String After Appending: ", s_1.data)
# Here, we will Remove from string
s_1.remove("a")
print("String after Removing: ", s_1.data)
Chip
The Original String: JavaTpoint String After Appending: JavaTpointing String after Removing: JvTpointing