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
String1 = 'Welcome to the Geeks World'
print ( "String with the use of Single Quotes: " )
print (String1)
String1 = "I'm a Geek"
print ( "\nString with the use of Double Quotes: " )
print (String1)
|
Output:
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
Collections.UserString
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.
Syntax:
collections.UserString(seq)
Example 1:
Python3
from collections import UserString
d = 12344
userS = UserString(d)
print (userS.data)
userS = UserString("")
print (userS.data)
|
Output:
12344
Example 2:
Python3
from collections import UserString
class Mystring(UserString):
def append( self , s):
self .data + = s
def remove( self , s):
self .data = self .data.replace(s, "")
s1 = Mystring( "Geeks" )
print ( "Original String:" , s1.data)
s1.append( "s" )
print ( "String After Appending:" , s1.data)
s1.remove( "e" )
print ( "String after Removing:" , s1.data)
|
Output:
Original String: Geeks
String After Appending: Geekss
String after Removing: Gkss
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Aug, 2021
Like Article
Save Article