this article describes the indexing usage of Python slices. Share for you for your reference, specifically as follows:

in Python, you can use simple square brackets and a subscript to access each element of the sequence, which is called the slicing operator, the slicing operators have three forms:

[], [:], [: ">index is the offset of the access element, the positive number, the 0< =index< =len (sequence) -1; when the negative index is used, the scope is

 print ('a','b','c','d') [2] 

is an example of several ways to access multiple elements. Bcdefgh "print len 'len (sequence) display sequence length print sequence complete sequence print sequence[:] print sequence[2:3] segmentation slices show that the default step length is not specified, and the step length (here is setp is 2). E[0:3] print sequence[: 3] print sequence[2:8] print sequence[has been accessed from second elements to the last element print sequence[2:] print sequence[: Ence) print min (sequence) print sequence.index ('c')

here is a repeat operator *

sequence * copies_int

, when multiple copies of a sequence are needed, the repetition operator can play a role. > you must use the repeat operator

connection operator +

sequence1 + sequence2

for the plastic

 print sequence*3. Convenient, but this operation is not the fastest or most effective. For strings, this operation is not better than putting all the strings in a list or an Iterable object, and then using the join ()  method to connect all the contents together to save the internal memory; for the list, the recommendation is a column. The extend ()  method of the table type combines two or more list objects with the 

 str.join (sequence) method to connect the elements in the sequence to a new string 
 STR = "-"; SEQ = ("a", "B," "); The output of 

is:

a-b-c

 list.extend (SEQ) function for adding multiple values in another sequence at the end of the list (the original list extended by the new list) 
 aList = [123. (bList) print "Extended List:", aList; 

output is:

Extended List: the syntax of the

Extended is much more flexible than the simple single element index method, and the beginning and end of the index value can exceed the string. Length. That is, the initial index value can be less than 0, and the end index value can be greater than the length of the sequence, such as

 print ('Faye','Leanna','Daylen') [-100:100] 

output as:

. Way to display: every time the last character is cut off, how to achieve it?

 sequence ='abcdef'I = -1 for I in range (-1, -len) does not display the first, that is, no complete string is displayed, apart from a separate definition. sequence[: 0], using None as the index value 

 sequence ='abcdef'for I in [None] + range. Readers who are interested in content can view the topic of the site: "Python data structure and algorithm tutorial ," Python function use skill summary  "," Python file and directory operation skills summary  "

hope that this article will help everyone Python program design.

This paper fixed link:http://www.script-home.com/example-of-python-slice-index-usage.html | Script Home | +Copy Link

Article reprint please specify:Example of Python slice index usage | Script Home

You may also be interested in these articles!