Python文字列の切り取り原理、下付きの位置図

980 ワード

Python文字列の切り取りはいつもぼんやりしていて、公式サイトから図を見つけて、Python文字列がどのようにマークされているかを理解して、具体的な意味の図は以下の通りです.
 
 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

 
>>> word[:2]  # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]  # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'