Just subclass the type
>>> class X(str):
... def my_method(self):
... return int(self)
...
>>> s = X("Hi Mom")
>>> s.lower()
'hi mom'
>>> s.my_method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in my_method
ValueError: invalid literal for int() with base 10: 'Hi Mom'
>>> z = X("271828")
>>> z.lower()
'271828'
>>> z.my_method()
271828
Related Contents:
- Is there a way to convert number words to Integers?
- What does the ‘b’ character do in front of a string literal?
- How can I concatenate str and int objects?
- How do I execute a string containing Python code in Python?
- How do I get a substring of a string in Python?
- Convert a Unicode string to a string in Python (containing extra symbols)
- How do I get the filename without the extension from a path in Python?
- How to extract the substring between two markers?
- Best way to replace multiple characters in a string?
- What is the difference between a string and a byte string?
- Making object JSON serializable with regular encoder
- Can you monkey patch methods on core types in Python?
- Finding all possible permutations of a given string in python
- add a string prefix to each value in a string column using Pandas
- How can I split and parse a string in Python?
- Why are empty strings returned in split() results?
- String concatenation without ‘+’ operator
- Check if string ends with one of the strings from a list
- How do I split a multi-line string into multiple lines?
- Cannot concatenate ‘str’ and ‘float’ objects?
- Why are str.count(”) and len(str) giving different output?
- Efficient way to add spaces between characters in a string
- str.strip() strange behavior
- How to convert an int to a hex string?
- Joining pairs of elements of a list [duplicate]
- What does a leading `\x` mean in a Python string `\xaa`
- Keeping only certain characters in a string using Python?
- Finding a substring within a list in Python [duplicate]
- How do I put a variable’s value inside a string (interpolate it into the string)?
- Why doesn’t calling a string method (such as .replace) modify (mutate) the string? Why doesn’t it change unless I assign the result?
- How to write very long string that conforms with PEP8 and prevent E501 [duplicate]
- Count number of words per row
- Run length encoding in Python
- How to convert a string with comma-delimited items to a list in Python?
- What is a clean way to convert a string percent to a float?
- Difference between using commas, concatenation, and string formatters in Python
- How can I extract keywords from a Python format string?
- Why doesn’t calling a string method (such as .replace or .strip) modify (mutate) the string?
- How do I reverse a string in Python?
- Python: is using “..%(var)s..” % locals() a good practice?
- How to apply __str__ function when printing a list of objects in Python [duplicate]
- How to explain the str.maketrans function in Python 3.6?
- Python regex: splitting on pattern match that is an empty string
- How do I check if a given Python string is a substring of another one? [duplicate]
- How to split a Python string on new line characters [duplicate]
- pandas: replace string with another string
- Determine precision and scale of particular number in Python
- pandas combine two strings ignore nan values
- Setting the fmt option in numpy.savetxt
- How to Print “Pretty” String Output in Python