# Comments are preceded by the hashtag (pound sign) # Comments extend from the hashtag off to the end of the line numeralCharacters = '0123456789' # assign a string of characters to numeralCharacters print 'numeralCharacters: ' + numeralCharacters # print the value of the string variable numeralCharacters # Assign the first 3 characters of numeralCharacters to first3Chars # Note that the numbers in square brackets are 0-base indexes. # Position 0 in the string is occupied by the first character. # Think of an index as being the distance, in characters, from the beginning # of the string. This will also be useful for other kinds of data later on. # In numeralCharacters, the first character in the string is '0' and the last # character is '9'. To extract a range of characters from a string and # optionally assign them to another, use a 'slicing' operation like this: first3Chars = numeralCharacters[0:3] # where [0:3] following the string variable says 'starting at position 0, # extract the characters up to but not including the character in position 3. # Now we can print the result to see how python interpreted our code: print 'first 3 characters: ' + first3Chars # Now lets pull out characters from the middle of numeralCharacters: middleChars = numeralCharacters[4:7] # This can get confusing so let's thing about this very carefully by mapping # out each character in numeralCharacters to a position: # Position: 0 1 2 3 4 5 6 7 8 9 # Value: 0 1 2 3 4 5 6 7 8 9 # Therefore, numeralCharacters[4:7] extracts the following range: # Position: 0 1 2 3 4 5 6 7 8 9 # Value: - - - - 4 5 6 - - - # Why does the range go up to 7 but not include it? I ask myself the same # question all the time. That's just how the operation is defined. # Fortunately, similar operations in other programming languages # behave the same way. Let's print the results, though, just to make sure: print 'middleChars: ' + middleChars