1

I'm looking for the most efficient way to compare two strings, and I'm not sure which is better: == or in. Or is there some other way to do it that is more efficient that either of these?

Edit: I'm trying to check for equality

wolfy
  • 113
  • 1
  • 7
  • Until you run into a known performance issue with your code when comparing strings, pick one over the other. Idiomatically I've seen `==` but again it depends on what it is you're doing. – Makoto Jul 16 '19 at 14:24
  • 1
    You seem to be asking something like "which is more efficient, an apple or an orange?". `in` and `==` don't do the same thing. – John Coleman Jul 16 '19 at 14:25
  • @JohnColeman Gotcha. What other ways are there for testing for equality? – wolfy Jul 16 '19 at 14:28
  • @wolfy you don't need other ways to test for equality, use `==`. "There should be one-- and preferably only one --obvious way to do it."-- Tim Peters, Zen of Python – Chris_Rands Jul 16 '19 at 14:29
  • I can't imagine a good use-case for something other than `==` for testing for string equality. – John Coleman Jul 16 '19 at 14:29
  • @Chris_Rands Thanks! I'll give that a read – wolfy Jul 16 '19 at 14:30
  • You might find [this question](https://stackoverflow.com/q/43466106/4996248) of interest. – John Coleman Jul 16 '19 at 14:35

3 Answers3

4

They do different things.

== tests for equality:

"tomato" == "tomato"  # true
"potato" == "tomato"  # false
"mat"    == "tomato"  # false

in tests for substring, and can be considered a (probably) more efficient version of str.find() != -1):

"tomato" in "tomato"  # true
"potato" in "tomato"  # false
"mat"    in "tomato"  # true  <-- this is different than above

In both cases, they're the most efficient ways available of doing what they do. If you're using them to compare whether two strings are actually equal, then of course strA == strB is faster than (strA in strB) and (strB in strA).

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

Please define "comparing".

If you want to know if 2 strings are equal, == is the simplest way.

If you want to know if 1 string contains another, in is the simplest way.

If you want to know how much they overlap, considering gaps, you need complicated algorithms. How about a thick book on algorithms? (This is similar to comparing genetic sequences. I think a book on Bioinformatics algorithms would be very useful too. Anyhow, this case is way too complicated for Stack Overflow.)

EDIT:

For equality stick with "==". It's in Python to do its job.

Arthur
  • 118
  • 1
  • 9
0

== in Python is there for comparison purpose, while "in" has a wider definition (contains which includes comparison). Generally, precise and clear purpose constructs are the most optimized ones for doing the targeted job, because indirect constructs are generally based on simple and direct constructs, which should make == better in comparison context and less error-prone.

Youcef4k
  • 338
  • 2
  • 13