0

I wondering why the following tests return False: Suppose I have 2 simple strings:

str0 = "trade"
str1 = "`trade"

I don't understand why the following tests in python return False:

str1.replace("`", "") is str0 

And,

"".join(list(str1)[1:]) is str0 => False

Thanks for your education!

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
H T
  • 1
  • 1
    `is` is not the same as `==` https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is – pault Sep 25 '21 at 00:51
  • 1
    This is due to an optimization in the interpreter. Always use `==` to compare values! – Klaus D. Sep 25 '21 at 00:55
  • 1
    _almost_ always use `==` to compare values. `None`, `True` and `False` are all singletons with a single identity. – tdelaney Sep 25 '21 at 00:58

1 Answers1

1

In Python, is compares two objects in memory, == compares their values. Since your two variables are stored at two different places in memory, your comparison using is evaluates to false.

kelortondo
  • 81
  • 4