Checkpoint 3.5.1.
- 11
- The blank counts as a character.
- 12
- Yes, there are 12 characters in the string.
What is printed by the following statements?
s = "python rocks"
print(len(s))
len
function, when applied to a string, returns the number of characters in a string.IndexError: string index out of range
. The reason is that there is no letter at index position 6 in "Banana"
. Since we started counting at zero, the six indexes are numbered 0 to 5. To get the last character, we have to subtract 1 from the length. Give it a try in the example above.fruit[-1]
yields the last letter, fruit[-2]
yields the second to last, and so on. Try it! Most other languages do not allow the negative indices, but they are a handy feature of Python!s = "python rocks"
print(len(s))
s = "python rocks"
print(s[len(s)-5])
s = "python rocks"
print(s[-3])