I came to know that any object in python has the same id irrespective of the place it is used.
That statement is completely false.
Small integers have their ID based on their immutable value and first occurrence in the program, because their values are small and Python caches them. That is why your first example returned True
.
However, in your second example, you are comparing the IDs of two different (immutable) Strings, and that's why it returns False
. In general, a new String (literal) instance creates a new String object each time, hence creating a different ID.