""" Purpose: introduce string slicing """ # Get string of choice s = input( "Enter favorite pie: " ) # Get indices i and j t = input( "Enter two indices i and j: " ) i, j = t.split() i = int( i ) j = int( j ) # slicing operator: string[ index1:index2 ] # the colon allows you to grab more than 1 character at a time # getting positions is really important, so python made the [] operator for that purpose # Get slices of s slice1 = s[ i : j ] # Substring starts at index i, ends at index j-1 slice2 = s[ i : ] # Substring starts at index i, continues to end of string slice3 = s[ : j ] # Substring starts at index 0, ends at index j-1 slice4 = s[ : ] # Substring equals the entire string # GOTCHA: slicing is [inclusive:exclusive] , inclusive of the first index, but exclusive of the second # Print results print() print( "s[ i : j ]:", slice1 ) print( "s[ i : ]:", slice2 ) print( "s[ : j ]:", slice3 ) print( "s[ : ]:", slice4 ) # second to last character in a string: # n = len( s ) # last_index = n - 1 # second_to_last_index = n - 2 # second_to_last = s[second_to_last_index] # when will we use this type of thing? all the time... this won't go away # uses include parsing, breaking up lists, etc. # there is a way to do almost everything using python... it's a very powerful language # the way might not be one function/method/operator, but there is a way to do almost everything # we have a lot of tools left to explore