"Learning Python with You Hand in Hand" 8-String Slicing

​In the last article "Learning Python with You Hand in Hand" 7-String Indexing , we have a preliminary understanding of the position indexing method of strings. The reason why index and slicing are written separately, as mentioned in the previous article, is to make the method and rules of slicing clearer, which will help us deal with different data structures in the future and deal with more complex slices. the way.

Simply put, an index corresponds to a piece of content (letters, numbers, symbols, etc.), and a slice corresponds to a piece of content.

Unlike the single position representation of an index, slices have a basic syntax expression:

object[start_index:end_index:step]

This object can be the string we are talking about, or it can be a list, array, DataFrame, etc. we will talk about later. It can be seen from this that their slicing methods are the same, so we must learn the basic skills well.

Star_index and end_index represent the starting point and focus of the slice. They are not exactly the same as counting from left to right and counting from right to left mentioned in our previous article. They have high flexibility. It should be emphasized that end_index is not included, which is similar to the concept of left closed and right opened in mathematics.

For example, str[0:5] represents the content corresponding to the 0th, 1, 2, 3, and 4 positions of the string from left to right, but does not include the content corresponding to the position where the end_index is 5.

There is another rule here, that is, when encountering a slice containing a head or a tail, the index position of the head and the tail can be ignored, then str[0:5] can be written as str[:5], str[- 10:-1] can be written as str[:-1].

But str[-5:-1] and str[-5:] are not the same. The former does not include the position of -1, but the latter does include, that is, until the last character of the string (behind There are examples).

As smart as you, if you want to select the entire string, you can write str[:].

step is the interval step length of each sampling when slicing, that is, how many values ​​take one value, the default is 1. When we don't input this parameter, we take one value according to each value from left to right, which means continuous value.

The step parameter is very important for slicing, because it not only determines the step size of the value, but also the direction of the value.

When the step is positive, the slice is taken from left to right, and the position of star_index and end_index is also required to be from left to right, that is, the starting point should be on the left of the ending point.

When the step is negative, the slice is taken from right to left, and the position of star_index and end_index is also required to be from right to left, that is, the starting point should be on the right of the ending point.

Regardless of whether the position index is a positive number or a negative number, or even a mixture of positive and negative numbers, as long as the positive and negative values ​​of the start position, end position, and step length meet the above requirements.

If it is not satisfied, although no error will be reported, an empty string will be returned.

In order to illustrate the above rules more intuitively, the following example diagram is drawn, and a string is used as an example to explain the more obscure definition above.

Consider this simplest string, str = '0123456789'. Its value is the same as the position index from left to right, which allows us to see the slice result more clearly.

When step=1: (default value, you can write it out or not, here it is not written out according to the convention)

In [1]: str = '0123456789'
 
In [2]: str[0:5]   # 起点、终点均为正值
Out[2]: '01234'
​
In [3]: str[-5:-1]   # 起点、终点均为负值
Out[3]: '5678'
​
In [4]: str[-5:]   # 起点为负值,终点省略,可以取到字符串最后一个值
Out[4]: '56789'
​
In [5]: str[2:-6]   # 起点为正值,终点为负值
Out[5]: '23'
​
In [6]: str[-8:6]   # 起点为负值,终点为正值
Out[6]: '2345'
​
In [7]: str[8:3]   # 起点在终点右侧
Out[7]: ''
​
In [8]: str[-2:-8]   # 起点在终点右侧
Out[8]: ''
​
In [9]: str[8:-10]   # 起点在终点右侧
Out[9]: ''
​
In [10]: str[-1:8]   # 起点在终点右侧
Out[10]: ''
​
In [11]: str[-1:9]   # 起点和终点一个位置,但终点不包括在内
Out[11]: ''

When step=-1:

In [12]: str[8:3:-1]   # 起点、终点均为正值
Out[12]:'87654'
​
In [13]: str[-2:-8:-1]   # 起点、终点均为负值
Out[13]:'876543'
​
In [14]: str[8:-10:-1]   # 起点为正值,终点为负值
Out[14]: '87654321'
​
In [15]: str[8::-1]   # 起点为正值,终点省略,可以取到字符串第一个值
Out[15]: '876543210'
​
In [16]: str[-1:8:-1]   # 起点为负值,终点为正值
Out[16]: '9'
​
In [17]: str[0:5:-1]   # 起点在终点左侧
Out[17]: ''
​
In [18]: str[-5:-1:-1]   # 起点在终点左侧
Out[18]: ''
​
In [19]: str[2:-6:-1]   # 起点在终点左侧
Out[19]: ''
​
In [20]: str[-8:6:-1]   # 起点在终点左侧
Out[20]: ''
​
In [21]: str[-1:9:-1]   # 起点和终点一个位置,但终点不包括在内
Out[21]: ''

The above uses 20 sentences to traverse the combination of different signs of the starting point, ending point and step length, and the final result.

The "#" after each sentence is a comment symbol to explain the meaning of the sentence and will not affect the operation.

You may feel a little dizzy at first. Please patiently verify one by one. If there are other scenarios that are not covered in the above example, please test by yourself.

If you can rely on your own calculations to get the same correct results, then congratulations to everyone. It means that your knowledge in this part is already solid enough to deal with any situation you will learn or encounter in the future.

The above examples are all about continuous slices, that is, step=1 or -1. If you change the value of step, discontinuous slices can be realized.

In [22]: str[0:5:2]
Out[22]: '024'
​
In [23]: str[-5:0:-3]
Out[23]: '52'

At this point, the slicing of strings is also finished. Maybe this is the first more convoluted topic we have encountered since learning Python, but the concept should not be complicated. As long as you can be patient to count a few times by yourself, you will be familiar. .

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" public account, read other articles in the "Learning Python with You Hand in Hand" series, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

For Fans: Follow the "also said Python" public account and reply to "Hand 8" to download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98755325