python - format() in the output string -


i have code:

print "{0} {1}".format("bruce","name") 

and expected output:

bruce name 

now, modified code to:

print "{1} {2}".format("bruce","name") 

and got indexerror: tuple index out of range.

so, supposed start numbering 0? new python programming appreciated.

yes, in python indexing sequences starts @ 0.

since str.format() parameters have 2 values there no value interpolate index 2, 0 , 1, indexerror thrown.

in general, sequence of elements, indices run 0 through len(sequence) - 1. sequence 2 elements, last index 1, length 42, last element found @ index 41, etc.

also see why python uses 0-based indexing, blogpost guido van rossum, creator of python, explains why python indexing starts @ 0, not 1.

note don't have number slots (unless stuck python 2.6 or 3.0). can omit numbers , python auto-number them you:

print "{} {}".format("bruce", "name") 

Comments