''' 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 = the first index, j = the second index i, j = int( i ), int( j ) print( 's:', s ) print( 'i:', i ) print( 'j:', j ) print() # string indexing: some_string[ index ] --> returns character at position index # where index is an int in range 0 --> len(some_string) - 1 print( 's[ i ]: ', s[ i ] ) print( 's[ j ]: ', s[ j ] ) print() # get slices of s using the slicing operator : # get a substring starting from position i and go up to but not including the character at index j # so includes character at position i and up to index j - 1 # if i = 3 and j = 15 --> includes all characters starting from index 3 through character at index 14 slice1 = s[ i : j + 1 ] # we store the substring in a variable so we can use it later # the second number after : is optional. If you don't give it a second number, then it goes all the way to # the end of the string s slice2 = s[ i : ] # also optional to put a number in front of : --> then start at the very beginning (0) and up to # but not including j slice3 = s[ : j ] # also both numbers are optional --> makes a new copy of the entire string! Starts at index 0 and goes up to # the end of the string s slice4 = s[ : ] # i and j are just variables that hold integers # we can substitute them for actual numbers # if s = 'apple' # index 01234 # s[ 1 : 3 ] = 'pp' # s[ : 3 ] = 'app' # s[ 1 : ] = 'pple' # s[ : ] = 'apple' # strings are immutable! Cannot change anything, only return a new copy print( 's[ i : j + 1 ]:', slice1 ) print( 's[ i : ]:', slice2 ) print( 's[ : j ]:', slice3 ) print( 's[ : ]:', slice4 ) # to include j, you can do j + 1 in the substring slicing # if i = 0 and j = 0 # s[ i : j ] --> goes up to but not including 0 is just an '' because doesn't include 0!