Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

omri lahav
omri lahav
434 Points

why I always hear strings are immutable

what does the mean of strings are immutable, if i can change the string by adding something else to it if i use it again?

for ex.

first_name = "omri"

first_name = first_name + "lahav"

omri lahav

i just changed the string so what so immutable here?

2 Answers

Steven Parker
Steven Parker
217,531 Points

This code isn't actually changing the original string. It's building a new string by copying the original one along with the additional characters, and then reassigning the same variable with the new string.

You can demonstrate the immutability of strings by trying to change just one character inside one, for example:

first_name = "omri"
print(first_name[1])    # this will show "m"
first_name[1] = "x"     # but this will fail
omri lahav
omri lahav
434 Points

ho ok thank you very much now i get it :)