Access by Offset

来源:互联网 发布:新版淘宝不能指纹支付 编辑:程序博客网 时间:2024/06/16 00:36
Access by Offset

Great work! (By the way, you could also have repaired the string by replacing the single quotes on the ends with double quotes, like this: "Help! Help! I'm being repressed!".)

Remember how we told you that strings were, technically speaking, strings of characters? Wouldn't it be nifty if you could get to each character in a string individually?

Well, you can!

Each character in a string has a subscriptor offset, which is a fancy way of saying it has a number attached to it. The number starts at 0 for the leftmost character and increases by one as you move character-by-character to the right. Check out the diagram in the editor!

"""The string "PYTHON" has six characters,numbered 0 to 5, as shown below:+---+---+---+---+---+---+| P | Y | T | H | O | N |+---+---+---+---+---+---+  0   1   2   3   4   5So if you wanted "Y", you could just type"PYTHON"[1] (always start counting from 0!)"""fifth_letter ="MONTY" fifth_letter=fifth_letter[4]print fifth_letter


When you think you've got the hang of the code in the editor, setfifth_letter equal to the fifth letter of the string "MONTY", like so:"MONTY"[?] (but replace the ? with the correct number).