0

Related to the replace function... I think it's inconsistent with the return value.

The replace function is defined as:

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

But...

->>> myStr = 'aaa'
->>> id(myStr.replace('a','c'))
1524508142928
->>> id(myStr.replace('b','c'))
1524508142760
->>> id(myStr)
1524508142760

The definition fits only when it's actually replacing something. What do you think about this?

Regards

Alin
  • 1
  • 1

1 Answers1

0

Because string is immutable type so that is the reason you are getting a different memory location after replacement.When you do any kind of modification to string variable it will create new one.