#-*-coding:utf-8-*-#string Slicesnames="ABCDEFGH"" "Slice Syntax names[start position: End Position: Step] Start Position: That is, the subscript of the string, can be a positive sequence subscript (0,1,2 ...), can also be reverse subscript ( -1,-2,-3 ...) Terminating position: is also the subscript of a string, but unlike the starting position subscript, the element pointed at the terminating position is not included in the inner stride: The default value is 1, when the step >0, which means traversing from left to right, when the step is <0, indicates that the value of the start position or the terminating position is traversed from right to left. Then the direction of the traverse is determined by the positive or negative of the step" "Print(Names[2:5])#Print the value of CDE without names[5]Print(Names[2:6])#Print Cdef#intercept from E to end of stringPrint(names[4:])#Print Efgh#truncate to the second-to-last character starting with EPrint(names[4:-1])#print string at intervalsPrint(Names[::2])#Print Aceg#Reverse Print stringPrint(Names[::-1])
Python string slices