When you define your function, you Python creates a lone function object called stripped
. It doesn't add your function to the builtin str
object. You just
need to call your method on the string normally:
>>> def stripped(x):
x = x.replace(' ', '')
>>> string = " yes Maybe So"
>>> stripped(string)
>>>
Note however string
will not be modified, You need to return the result of x.replace()
and assign it to string
:
>>> def stripped(x):
return x.replace(' ', '')
>>> string = " yes Maybe So"
>>> string = stripped(string)
>>> string
' yes Maybe So'
>>>
Note what your asking for is techinally possible. It is a monkey-patch however and shouldn't be used. But just for completeness:
>>> _str = str
>>>
>>> class str(_str):
def stripped(self):
return self.replace(' ', '')
>>> string = str(" yes Maybe So")
>>> string.stripped()
' yes Maybe So'
>>>