Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrew Hiser
493 PointsWhat do you (Craig/video instructor) mean when you say that strings can't be changed?
If it's a variable, shouldn't it be able to be changed, as they can in other languages? Also, when I test this, I can change strings by assigning new values to them with "=". I think I'm missing something here.
2 Answers
Kailash Seshadri
3,087 PointsWell a string is just a number of characters in any order, for example; "I like apples". Strings can be stored in a variable (in this casestatement
):
statement = "I like apples"
When you use an = to change the string, e.g. saying statement = "I like bananas"
later in the code (as shown below), you arent really changing the string "I like apples". Instead, you are actually deleting the initial string "I like apples" from the variable statement
putting a brand new string, "I like bananas" in its place.
statement = "I like apples"
statement = "I like bananas"
You cannot change strings by deleting specific letters or characters in it directly (i.e. converting the string from "I like apples" to "I lke ppls" by removing the vowels directly). You can however, replace it with a new string as I showed earlier.
Hope this helps, and If you have any other questions, feel free to ask me below!
Mark Casavantes
10,619 PointsPython strings are "immutable" which means they cannot be changed after they are created.