For Loops w/ Strings & Lists
Both strings and lists are indexable, so we can access individual items by indicating an index with square brackets.
Examples:
Examples of indexing with a For Loop
recall that
range()
never includes the last valueindexing in Python always starts at 0(zero).
Therefore, a string of:
Hello
has its last character at index of 4, but has a length of 5Same idea applies to lists as well.
Accessing items in a String or a List without indexing
We can also just iterate through strings or lists by using a for loop without the need for the index values.
Last updated