Python vs str string module

>>> import string >>> s = 'happy cat' >>> string.find(s, 'cat') 6 

and

 >>> s = 'happy cat' >>> s.find('cat') 6 

In the above two parts of the code, I have the following doubts.

  • Why does the second code work without importing the string module?
  • Is there a performance improvement when using one over the other?

Thanks, Winnie

+4
source share
1 answer

The functions defined in the string module, which are currently str methods, are deprecated in Python 2.4 and should not be used at all, although they were stored in later versions of Python 2 for backward compatibility. They were removed in Python 3.0.

  • Why does the second code work without importing the string module?

Because it is a method like str .

  1. Is there a performance improvement when using one over the other?

Well, string.find(x, y) calls x.find(y) , but performance doesn't matter here (see first sentence).

+10
source

Source: https://habr.com/ru/post/1389415/


All Articles