Solution: Define a Class that Performs Operations on a String
Learn how to write a class that performs operations on a string in Python.
We'll cover the following...
The solution to the problem of defining a class that performs operations on a string in Python is given below.
Solution
Press + to interact
class String :def __init__(self, x) :self.s = xdef getString(self) :return self.sdef __iadd__(self, other) :self.s = self.s + other.sreturn selfdef toUpper(self) :self.s = self.s.upper( )def toLower(self) :self.s = self.s.lower( )a = String('www.abcd')b = String('.com')a += bprint("Concatenated string:",a.getString())a.toUpper( )print("Lower to upper:",a.getString())a.toLower( )print("Upper to lower:",a.getString())