Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does slicing in python mean?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the meaning of slicing in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I stole the following from the Internet

A complete slicing expression contains two ":" to separate three parameters (start_index, end_index, step). When there is only one ":", the default is the third parameter, step=1.

Step: both positive and negative numbers are available, and their absolute values determine the''step size' when fetching data, while a positive or negative sign determines the "cutting direction", with a positive value "from left to right" and a negative value "from right to left". When step is omitted, the default is 1, that is, the value is incremented by 1 from left to right. " It is very important to take direction! " It is very important to take direction! " It's very important to cut the direction! "say the important things three times!

Start_index: represents the starting index (including the index itself); when this parameter is omitted, it means that the value starts from the "endpoint" of the object, and whether to start from the "starting point" or from the "end point" is determined by the positive or negative of the step parameter. Step is positive from the "starting point" and negative from the "end point".

End_index: indicates the termination of the index (excluding the index itself); when this parameter is omitted, it means that it is taken all the way to the "endpoint" of the data, and whether it is to the "starting point" or to the "end point" is also determined by the positive or negative step parameter, when step is positive until the "end point", and when it is negative until the "starting point".

Link: https://www.jianshu.com/p/15715d6f4dad

This is my understanding at the beginning.

The code is as follows:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32

Str2 = 'abc123456'

Print (str2 [2:: 2])

C246

Print (str2 [2:: 3])

C36

Print (str2 [1:: 2])

B135

Original string: str2 = 'abc123456'

Slicing operation: result c246 of str2 [2VRV 2]

Result c36 of str2 [2VRV 3]

Result b135 of str2 [1 VuGV 2]

By comparison, we can see that:

When the start index is 2 and the end index is empty to the end, step 2, the original string is actually sliced from the subscript with index 2 as the beginning of the slice, and the final result is similar to the section seen after cutting vegetables. The result is c246, because it is exposed after cutting from left to right.

This is the end of the content of "what is the meaning of slicing in python"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report