A string is an ordered collection of characters, and specific elements can be obtained by their position. In Python, characters in a string are extracted by index, starting from 0.
Python can use negative values, meaning extraction from the end. The last one is -1, the second to last is -2, i.e., the program considers counting backwards from the end.

The following is a summary of Python indexing and slicing:
1. Indexing gets elements at specific offsets
- The offset of the first element in a string is 0
- The offset of the last element in a string is -1
- str[0]Get the first element
- str[-2]Get the second to last element
2. Slicing extracts corresponding parts of data
- Usually the upper bound is not included in the extracted string
- If no value is specified, the slice boundaries default to 0 and the length of the sequence
- str[1:3] Get the string from offset 1 to offset 3, excluding offset 3: "tr"
- str[1:]Get from offset 1 to the last character of the string (including the last character): "tring"
- str[:3]Get from offset 0 to offset 3, excluding offset 3: "str"
- str[:-1]Get from offset 0 to the last character (excluding the last character): "strin"
- str[:]Get all elements of the string from beginning to end: "string"
- str[-3:-1]Get characters from offset -3 to offset -1, excluding offset -1: "in"
- str[-1:-3]andstr[2:0]Get an empty character; the system does not report an error: ""
- When slicing, you can also add a step length,str[::2]The output result is: "srn"