osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why list.reverse() modifies the list, but name.replace() does not


Hello all,

I am learning the basics of Python. How do I know when a method modifies the
original object, when it does not. I have to exmaples: Example 1:
> L = [3, 6, 1,4]
> L.reverse()
> L
[4, 1, 6, 3]
This changes the original list.

Example 2:
> name = "John Smith"
> name.replace("J", j")
> name
'John Smith'
This does not change the original string.

Why the two examples produce different results? As a beginner, I find this
confusing. How do you do it?

Thank you!