''' Purpose: remind you about string indexing. introduce string slicing ''' # operator [] is the sequence index operator. Our use so far # is to get an individual character from a string s = input( 'Enter favorite pie: ' ) t = input( 'Enter two indices: ' ) i, j = t.split() i, j = int( i ), int( j ) print( 's:', s ) print( 'i:', i ) print( 'j:', j ) print() print( 's[ i ]: ', s[ i ] ) print( 's[ j ]: ', s[ j ] ) print() # get slices of s using the slicing operator : # NEW: SLICING # We've seen subscripting (getting a single character in a string at the index) # Ex. s[0] # Slicing allows you to get MULTIPLE characters from a string at the indices # s[i:j] will get you the characters from index i to j - 1 # s = 'hello' # 01234 slice1 = s[ i : j ] # s[1:3] will give you 'el' (doesn't give you index 3 since we go to j-1 so index 2) so indexes 1 2 slice2 = s[ i : ] # s[1: ] will start at index i and give you everything from i onwards to the end so 'ello' so indexes 1 2 3 4 slice3 = s[ : j ] # s[ : 3] will start from the beginning and give you up to but not including index j 'hel' so indexes 0 1 2 slice4 = s[ : ] # gives you the whole thing (all indexes) 'hello' # so indexes 0 1 2 3 4 print( 's[ i : j ]:', slice1 ) print( 's[ i : ]:', slice2 ) print( 's[ : j ]:', slice3 ) print( 's[ : ]:', slice4 )