CSCE 110 1nd EditionLecture 3Outline of Last Lecture:I. VariablesII. Raw InputA. What are they?B. Key functionsIII. More on StringsOutline of Current Lecture:I. Subsets of StringsA. Indexes and SlicesB. Range SlicesCurrent LectureI. Subsets of StringsA. Indexes and SlicesThe separate characters of a string all have an assigned index. The index represents the placement/location of each character in a string. x = "python"The first index of every string is 0. We can also have negative values for indexes. Here is the complete breakdown of the string "python".Index # 0 1 2 3 4 0Index # -6 -5 -4 -3 -2 -1p y t h o nFor example, we could say the character "y" is in either 1 or -5. Note that even though the index range goes from 0 to 5, that the length is still 6 because there are six characters total in the string.We can call forth a character from a particular index. To do that, we write the name of the variable we wish to pull from, and in brackets, the index value we wish to pull the character from. Here are some examples of input/output. The output is a slice of the string.>>> x = 'python'>>> x[0]These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> 'p'>>> x[3+1]>>> 'o'Notice how we can use mathematical expressions to signify a particular index.>>> print x[-1]>>> nB. Range SliceTo pull multiple subsets of a string, we use a range slice. We still use brackets, butwe separate the indexes that represent the start and end of the range using a colon. Here are some examples of input/output for range slices:>>> x = "python">>> print x[0:4]>>> pyth0 is the starting index, and 4 is the ending index. The command will return the characters from (and including) index 0, stopping at the character that is before index 4. >>> print x[0:]>>> pythonIf we add a colon but do not enter an index number after it, the range slice will include every character from the starting index to the end of the string. >>> print x[:4]>>> pythSimilarly, if we do not add a starting index before the colon, the range will automatically start at the beginning of the string. >>> print x[:]>>> python>>> print x[-4:]>>> thon>>> print x[2:2]>>>This will give no output, since there is no range.We can also pull a group of characters from a string that aren't in the same order as they are in the string. To do this, within the brackets, we add a colon and the "step size." Here are some examples of input/output:>>> x = "python">>> print x[0:6:2]>>> ptoStarting with index 0 and ending at index 6, the command pulls every character after taking 2 steps. We can get the same output with the following:>>> print x[::2]>>> print x[::-1]>>> nohtypWe can also use operators. Here are some examples of input/output:>>> x = "python">>> print x[::-1] * 2>>>
View Full Document