The string intercepted by python is an ordered collection. We can use indexes to extract the desired characters. We can also use Python strings as a list of strings for better understanding.
The Python string list has two values.
1 is from left to right index default start 0, the maximum range is less than 1 String Length
S = 'ilovepython'
S [0] results in I
2 is from right to left index default-1 start, the maximum range is the start of the string
S = 'ilovepython'
The result of S [-1] is N.
The above is to get a character, if you actually want to get a broken character, you can usePython string TruncationFunction
For example
S = 'ilovepython'
S [] returns love
When a string separated by colons is used, Python returns a new object. The result contains the continuous content identified by the offset. The start of the left part contains the lower boundary, for example
The above result contains the value L of S [1], and the maximum obtained range does not include the upper boundary, that is, the value p of S [5 ].
Here are a few examples to help you better understand:
S [: 3]
The result is 'ilo', saving: If the left side is left, the index value starting from the left side is 0. In fact, this expression is the same as s [0: 3.
S [:-1]
The result is that the index value starting from the left side is 0, and the maximum value is the last character in the string, but not the last character.
S [:]
The result is: 'ilovepython 'obtains the element from the offset 0 to the end. This is a python string copy. You can better understand Python reference and copy here.
For more information about Python strings, see: Replacement of Python strings and interception of Python strings.
Author: Lao Wang @ Python tutorial
Old Wang Python provides Python tutorials and Python downloads related to pythn.