Can you compare strings in python like in Java with .equals?
Question:
Can you compare strings in Python in any other way apart from ==
?
Is there anything like .equals
in Java?
Answers:
You could do:
import operator
a = "string1"
b = "string2"
print operator.eq(a, b)
This is similar to Java in that you’re not using an explicit operator.
However in Java you’re using a method call on the String
class (i.e., myString.equals(otherString)
) but in Python eq
is just a function which you import from a module called operator
(see operator.eq
in the documentation).
There are two ways to do this. The first is to use the operator
module, which contains functions for all of the mathematical operators:
>>> from operator import eq
>>> x = "a"
>>> y = "a"
>>> eq(x, y)
True
>>> y = "b"
>>> eq(x, y)
False
>>>
The other is to use the __eq__
method of a string, which is called when you use ==
:
>>> x = "a"
>>> y = "a"
>>> x.__eq__(y)
True
>>> y = "b"
>>> x.__eq__(y)
False
>>>
According to the docs:
eq(a, b)
is equivalent to a == b
So ==
is just like .equals
in Java (except it works when the left side is null
).
The equivalent of Java’s ==
operator is the is
operator, as in:
if a is b
What is the need for using other than ‘==’ as python strings are immutable and memoized by default?
As pointed in other answers you can use ‘is’ for reference(id) comparison.
In Java, .equals()
is used instead of ==
, which checks if they are the same object, not the same value. .equals()
is used for comparing the actual values of 2 strings.
However, in Python, "==" by default checks if they have the same value so it is better to use in general.
As other solutions pointed out, you can also __eq__
as another way to get the same result.
Can you compare strings in Python in any other way apart from ==
?
Is there anything like .equals
in Java?
You could do:
import operator
a = "string1"
b = "string2"
print operator.eq(a, b)
This is similar to Java in that you’re not using an explicit operator.
However in Java you’re using a method call on the String
class (i.e., myString.equals(otherString)
) but in Python eq
is just a function which you import from a module called operator
(see operator.eq
in the documentation).
There are two ways to do this. The first is to use the operator
module, which contains functions for all of the mathematical operators:
>>> from operator import eq
>>> x = "a"
>>> y = "a"
>>> eq(x, y)
True
>>> y = "b"
>>> eq(x, y)
False
>>>
The other is to use the __eq__
method of a string, which is called when you use ==
:
>>> x = "a"
>>> y = "a"
>>> x.__eq__(y)
True
>>> y = "b"
>>> x.__eq__(y)
False
>>>
According to the docs:
eq(a, b)
is equivalent toa == b
So ==
is just like .equals
in Java (except it works when the left side is null
).
The equivalent of Java’s ==
operator is the is
operator, as in:
if a is b
What is the need for using other than ‘==’ as python strings are immutable and memoized by default?
As pointed in other answers you can use ‘is’ for reference(id) comparison.
In Java, .equals()
is used instead of ==
, which checks if they are the same object, not the same value. .equals()
is used for comparing the actual values of 2 strings.
However, in Python, "==" by default checks if they have the same value so it is better to use in general.
As other solutions pointed out, you can also __eq__
as another way to get the same result.