0

I was reading from the book How to Think Like A Computer Scientist: Learning with Python when I came across the following question:

As an exercise, describe the relationship between string.join(string.split(song)) and song. Are they the same for all strings? When would they be different? (song had been defined as "The rain in Spain...")

After checking it out, however, I found that both are different strings. I tried using string.join(string.split(song)) is song and f is song where f had been assigned the value string.join(string.split(song)) and both evaluated to False. Why is it so?

user117913
  • 101
  • 1
  • 1
  • 3

1 Answers1

3

What are the actual values of the strings you are comparing?

If both are the same, this is because of the difference between the identity operator is and the equality operator ==.

In short, is yields True when objects are identical. Because a new string is created in your example, it produces False.

If you'd use == a deep comparison of the strings' characters would take place and True would be returned.

If the compared strings are not the same, neither == or is should produce True.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88