In Python for loops iterate over a sequence, binding the variable to each element in the sequence. A range is basically a list of numbers, ie. range(5) is equivelent to [0, 1, 2, 3, 4]. Altering i within the loop will have no effect once the next iteration begins because it will be rebound to the next element in the sequence. To skip one iteration ahead you would use the continue statement, as in Oluseyi's example.
Because of the binding mechanics I just described, we usually iterate directly over the sequence rather than over the range of its indices as in C. A string is an iterable sequence in Python. Example:
for char in string: if char == '{': # etc, etc.
By the way, for
string formatting you don't need to know in advance what the variable name is. You only need to be sure there is a key for it in the provided dictionary. Example:
profile = {"name":"Chozo", "health":100, "ammo":20 }greet = "Hello %(name)s."status = "Your health is %(health)d and you have %(ammo)d rounds."print greet % profileprint status % profile
If the key is not gauranteed to exist you can handle the KeyError exception.
try: output = greet % profileexcept KeyError: output = greet % {"name":"noname"}
You can also get your local variables in dictionary form:
name = "Chozo"print greet % locals()
(ps. you shouldn't use str as a variable name because that just happens to the built-in type name for strings)