>>> import string
>>> s = 'happy cat'
>>> string.find(s, 'cat')
6
and
>>> s = 'happy cat'
>>> s.find('cat')
6
In the above 2 pieces of code, i have the following doubts.
Thanks, Vinay
The functions defined in string
module that are nowadays methods of str
were deprecated in Python 2.4 and should not be used at all, though they were retained in later Python 2 versions for backward-compatibility. They were removed in Python 3.0.
- Why is the 2nd code working without import the string module?
Because it's a method of str
type.
- Is there any performance improvement in using one over the other?
Well, string.find(x, y)
calls x.find(y)
, but the performance doesn't matter here (see first sentence).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With