Slice truncation is a common string operation in python. we will introduce it in detail in this article. the Function of slice truncation is to obtain subcharacters or substrings. In fact, what we need to do is to use an index to separate two indexes with a colon, in the form of: variable [header subscript: tail subscript], the number before the colon represents the start position, the number after the colon indicates the end position. This is a left-closed and right-open interval. that is to say, this string contains the header subscript, but does not contain the end subscript. There are two indexing methods for Python data: 0 at the leftmost and 0 at the rightmost... slice truncation is a common string operation in python. we will introduce it in detail in this article. the Function of slice truncation is to obtain subcharacters or substrings.
In fact, what we need to do is to use an index to separate two indexes with a colon, in the form of: variable [header subscript: tail subscript], the number before the colon represents the start position, the number after the colon indicates the end position. This is a left-closed and right-open interval. that is to say, this string contains the header subscript, but does not contain the end subscript.
There are two indexing methods for Python data: the leftmost index starts with 0 and Increases Sequentially; the rightmost element index is-1 and decreases sequentially from left to left.
The indexing of Python is flexible. you can select the corresponding indexing method based on the actual situation.
String index
You can use the index to obtain a character in the string. you can use the subscript [x] directly. do not forget that the index starts from 0!
For example, if a string is "Python", you can use language [1] and lanuage [-5] to obtain 'P '.
Split slice
The slice operation in Python often uses the split slice, that is, using the colon (:) in [] to split the string.
Take say_hell = 'hello' as an example: