And subscripts used in python slices

Subscript

The so-called index is the number, like supermarket storage cabinet number, through this number will be able to find the appropriate storage space.

Python, strings, lists, tuples support the subscript index.

E.g:

# If you want to remove the portion of the character, using the subscript 
name = " ABCD " 

Print (name [0])
 Print (name [. 1 ])
 Print (name [2 ])
 Print (name [. 3 ]) 

# output is: 
# A 
# B 
# C 
# D

 

slice

It refers to an operation of a portion of a slice of the operation of the object taken, strings , lists , tuples support slicing operation.

Grammar sections: [start: end: step]

Note : Select the closed section belonging to the left and right open type, that is, from the start "start" position to the "end" before the end of a bit (do not include the end of the bit itself).

We have an example to explain the string:

= name " abcdef " 

Print (name [0:. 3])   # remove the standard character is 0 to 2, the result is: ABC 

Print (name [0:. 5])   # remove the standard character from 0 to 4, the results of is: ABCDE 

Print (name [3:. 5])   # remove the standard 3-4 character, the result is: de 

Print (name [2:])   # remove the standard 2 is started until the last character, the result is : CDEF 

Print (name [0: -1])   # remove labeled zero to second last character, the result is: ABCDE 

Print (name [:: -. 1]) # reverse text, the result is: fedcba

 

 

Guess you like

Origin www.cnblogs.com/lxy0/p/11324190.html