''' Purpose: Remind you about subscript indexing. Introduce slicing ''' # operator [] is the sequence index operator. Our use so far in strings is to # get an individual character; and in lists to get an individual element value s = 'of to in is on by it or be at' words = s.split() print( 's =', s ) print( 'words =', words ) print() # get the middle charcter in string s ns = len( s ) i = ns // 2 mid_ch = s[ i ] # get the middle word in the list w nw = len( words ) i = nw // 2 middle_word= words[ i ] # operator [] can also be used to get a slice (substring) of a string and a # sublist of a list # get the middle half, 1st three quarters, last 3 quarters, and a copy i1 = ns // 4 i2 = 3 * ns // 4 s1 = s[ i1 : i2 ] s2 = s[ : i2 ] s3 = s[ i1 : ] s4 = s[ : ] print( 's[', i1, ':', i2, '] =', s1 ) print( 's[ :', i2, '] =', s2 ) print( 's[', i1, ': ] =', s3 ) print( 's[ : ] =', s4 ) print() # get the middle 3 fifths, 1st 4 fifths, last 4 fifths, and a copy j1 = nw // 5 j2 = 4 * nw // 5 w_slice1 = words[ j1 : j2 ] w_slice2 = words[ : j2 ] w_slice3 = words[ j1 : ] w_slice4 = words[ : ] print( 'words[', j1, ':', j2, '] =', w_slice1 ) print( 'words[ :', j2, '] =', w_slice2 ) print( 'words[', j1, ': ] =', w_slice3 ) print( 'words[ : ] =', w_slice4 )