String Manipulation W/ python

why/how is it that a variable can be assigned a value that is the same as the variable?
Whats the point?
(Line 8)

I don’t understand what you mean with ā€œa value that is the same as the variableā€, what do you mean?

oh, you mean the encrypted_text = encrypted_text +? you want to keep the old value of encrypted_text and add something new to it, not discard the old value and assign a completely new value. You can use encrypted_text += as a shortcut for that, this is called compound assignment

1 Like

Im still a little confused. The previous value of encrypted_text was blank (encrypted_text=ā€˜ā€™)
so what was the previous value?

Even if it’s blank you can still add to it.

In this case it will start off empty, the first character will be added, and then each subsequent character will be added to the previous.

The whole exercise is to break down a string into characters, shift them, then rebuild a new string character by character. So you keep adding to the final string one letter at a time.

1 Like

Sorry for asking so many questions:
So leaving the value empty, in this case, is due to the variable pretty much waiting for something to pass through it? And everything else was us setting up for this?

Yes, exactly!

Technically you don’t need to declare the variable ahead of time, it will be created when you assign it in the loop, but it’s good practice to declare all of your variables at the top, so you can see what you’re working with and plan your program.

In this case, it’s declared and assigned an empty string, so we know we have a string variable to work with.

Ask as many questions as you can think of!

1 Like

Just to be clear, here it’s not a matter of good practice. If you don’t declare the variable before, you got a NameError when you try to do something like:
variable = variable + something_else.

1 Like

Right, because the variable is on the other side of the operator here, thanks for pointing that out

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.