From Python's Official Documentation - 8.3 The for Statement
"The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list)..."
In your example of code, "Lucy"
is the string. The string is the sequence. The characters that compose it (i.e. "L", "u", "c", "y"
) are the "elements of a sequence."
I'm going to walk through your code line-by-line to see if it helps.
1.You assign sys.argv[1]
to the variable mysteryString
. Your sys.argv[1]
is the string "Lucy"
.
mysteryString = sys.argv[1]
2.The program takes the variable mysteryString
, formats the string with it, and prints the output.
print("~ testing with mysteryString = {} ~".format(mysteryString))
Output: ~ testing with mysterString = Lucy ~
3.This line initializes the variable charCount
. Note that it is empty.
charCount = ""
4.This line marks the beginning of the for-statement (aka for-loop). The for-statement iterates (or "loops over") every element in the provided sequence. In order to loop over a sequence, you must assign each element in it to a variable. Here, that variable is mysteryChar
. The first element of the sequence (i.e. the character "L"
) is assigned to variable mysteryChar
.
for mysteryChar in mysteryString:
If it helps, you can think of what is happening at this point as:
mysteryChar = "L"
5.This line does a couple of cool things. First, it takes the value of charCount
and adds it to the value of mysteryChar
. Second, it assigns the values of charCount
and mysteryChar
to charCount
.
charCount = charCount + mysteryChar
Remember when in step 3, we assigned the variable charCount
to an empty string?
charCount = ""
After step 5 executes:
charCount = "L"
6.This line prints charCount
.
Code:
print(charCount)
Output:
L
7.Now, try to follow the code.
Code:
charCount = ""
for mysteryChar in mysteryString:
charCount = charCount + mysteryChar
print(charCount)
Output:
L
8.Continue to next iteration of the loop.
Code:
mysterString = "Lucy"
# Note: Before the second iteration of the loop, charCount = "L"
# This is because in the first iteration of the loop,
# "L" was added & assigned to the variable charCount
charCount = ""
for mysteryChar in mysteryString:
charCount = charCount + mysteryChar
print(charCount)
Output:
Lu
9.Continue to the next iteration of the loop
Code:
mysterString = "Lucy"
# Note: Before the third iteration of the loop, charCount = "Lu"
# This is because in the second iteration of the loop,
# "u" was added & assigned to the variable charCount
# At that point, charCount = "Lu"
charCount = ""
for mysteryChar in mysteryString:
charCount = charCount + mysteryChar
print(charCount)
Output:
Luc
10.This is all of the for-loop's output.
Output:
L
Lu
Luc
Lucy
Question:
"Why does it print the characters the way it does"
The for-loop prints the characters the way it does because it loops over each element in the sequence individually.
First Iteration Output:
L
Second Iteration Output:
Lu
Third Iteration Output:
Luc
Fourth Iteration Output:
Lucy
I started working on learning python about a year ago, so this stuff is relatively new to me, too. Welcome critiques / corrections if my explanation is misinformed.