''' 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 = 'change' n = len( s ) # len( s ) gives you the length of the string (number of characters in the string) print( 's =', s, ) print( ' 0123456' ) print() print( 'n = len( s ) =', n ) print() print( '### If the [] operand is an integer value, it is subscripting' ) x = s[ 0.0 ] # Can't do this! y = s[ 4 ] # Can't subscript if the index doesn't exist in the string (ex. when you have length 3 and try to get index 4) z = s[ n-1 ] # WRITE THIS DOWN: # For a string, the first character is always at index 0 and the last character is at # len( string ) - 1 # 'CS1112' # 012345 # Length 6 # First character 'C' at index 0 # Last character '2' at index 5 (which is 6 - 1 = len('CS1112')-1) # Subscripting - Getting a character at an index in a string # Indexes are number positions for characters in a string! They're the number positions # where the characters are! print( 's[ 0 ] = ', x ) print( 's[ 4 ] = ', y ) print( 's[ n-1 ] = ', z )