''' Purpose: continue string manipulation introduction ''' print() # Operator [] is the string index operator -- it provides access to # a string subsequence. Depending upon its operands it is all called # subscripting and slicing. Here we only care about subscripting. # The subscript is an index value. In Python index values start at 0. print( "### [] is the string index operator" ) s = "obliviator" n = len( s ) print( "s =", s, ) print( " 0123456789" ) print() print( "n = len( s ) =", n ) print() # Remember this!! Indexes start at 0! # The last index is the length of the sequence - 1 # 10 characters in your string? the first letter is at index # 0 and the last character is at index (len(string)- 1) so at index 9 # If we tell you to find the last character in a string, # do NOT do s[len(s)] or s[n] if n = len(s) # TAKEAWAY: FIRST CHARACTER AT INDEX 0 LAST CHARACTER AT INDEX len(s)-1 # where s is a string. # s = "1112" # s[0] = '1' s[1] = '1' s[2] = '1' s[3] = '2' # s = "CS 1112" with a space! # s[0] = 'C' s[1] = 'S' s[2] = ' ' s[3] = '1' s[4] = '1' s[5] = '1' s[6] = '2' print( "### If the [] operand is an integer value, it is subscripting" ) x = s[ 0 ] y = s[ 4 ] z = s[ n-1 ] print( "s[ 0 ] = ", x ) print( "s[ 4 ] = ", y ) print( "s[ n-1 ] = ", z )