Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

What is the difference between find(target, key) and target.find(key)? They both seem to be syntactically correct in Python 2.7.2. However, neither produce output for me so I can't tell what difference it makes.

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at in under 10 minutes. Go to now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly


Get your free account and access expert answers to this and thousands of other questions

hmm... the string module, which is part of the standard library, has a find method. to use this method you have to import the string module. Python has an str class to which all strings belong (everything in Python is an object - "abcd" is an str object). The str class is a built-in sequence data type. 'built-in' means that all of the str methods are available to any str object without having to import anything. built-ins are probably more efficient at 'doing their job'. in the idle 'shell' try dir('abcd') it will return all the attributes of the 'abcd' object they both do the same thing, if you don't need any of the methods in the string module, you can use str methods and not have to import string. this is what i do, i don't know why - i have read a lot and decided to do things then forgotten what i read. :) you can actually open up standard library modules and see how they implemented things - they are .py files just like you have been writing. on my computer (windows) the module is at C:\Python27\Lib. careful not to change anything. when i first saw your post i was confused by "find(target, key)" til i realized you must have used 'from string import *' - if you think you might end up using python a lot, have a look at these
So you're saying that find(target, key) is used when you have imported the string module, and target.find(key) is used if you haven't imported the string module, right? And they both do the same thing, which is find the first occurrence of a key in a target.
Not quite Julie. It's a little confusing at first: There's a built-in string method called find, so every string has a find method, you can call it on a string, or on a expression that evaluates to a string: >>>'abc'.find('a') 0 >>>word = 'apple' >>>word.find('ppl') 1 >>>('abc' + word).find('p') 4 >>> There is also a module in the Standard Library called string, which contains a function called find. If you do from string import * you'll now have a function called find in your namespace which works on strings. This is a classic example of why you should be cautious doing `from x import *`. I totally agree with bwCA, It's all a bit spurious in general use. I'm sure there's some reason for it, but in practice, at least while your starting out, don't import anything to do a simple find on a string, just use the built-in string method.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Thanks- I'll stick to the basics without importing the string module.
No worries, I've seen the question asked a few times. It does confuse people. I gave the wrong answer first time I saw it asked, and a couple of us ended up going back over it. I don't think there was a delete feature at that time. I think bwCA might have been there, so that might be why he remembers the answer, but not the details; same as me. But yeah, I just use the built-in string method, I've never come across a problem with it so far.
thnx carlsmith that answer was a bit more coherent than mine
bwCA- the "do and don't" documents were very interesting. Thanks.

Not the answer you are looking for?

Search for more explanations.

Ask your own question