Strings are a sequence type, having characters ordered by index from left to right. An index is an integer matching a specific position in a string's sequence of characters. An individual character is read using an index surrounded by brackets. Ex: my_str[5]
reads the character at index 5 of the string my_str
. Indices start at 0, so index 5 is a reference to the 6th character in the string.
A programmer often needs to read more than one character at a time. Multiple consecutive characters can be read using slice notation. Slice notation has the form my_str[start:end]
, which creates a new string whose value contains the characters of my_str
from indices start to end -1. If my_str
is 'Boggle', then my_str[0:3]
yields string 'Bog'. Other sequence types like lists and tuples also support slice notation.
|
|
The last character of the slice is one location before the specified end. Consider the string my_str = 'John Doe'
. The slice my_str[0:4]
includes the element at index 0 (J), 1 (o), 2 (h), and 3 (n), but not 4, thus yielding 'John'. The space character at index 4 is not included. Similarly, my_str[4:7]
would yield ' Do', including the space character this time. To retrieve the last character, an end index greater than the length of the string can be used. Ex: my_str[5:8]
or my_str[5:10]
both yield the string 'Doe'.
Negative numbers can be used to specify an index relative to the end of the string. Ex: If the variable my_str
is 'Jane Doe!?', then my_str[0:-2]
yields 'Jane Doe' because the -2 refers to the second-to-last character '!', and the character at the end index is not included in the result string.
A list of common slicing operations a programmer might use.
Assume the value of my_str is 'http://en.wikipedia.org/wiki/Nasa/'
Syntax | Result | Description |
---|---|---|
| wikipedia | Returns the characters in indices 10-18. |
| wikipedia.org/wiki/ | Returns the characters in indices 10-28. |
| n.wikipedia.org/wiki/Nasa/ | Returns all characters from index 8 until the end of the string. |
| Returns every character up to index 23, but not including my_str[23]. | |
| Returns all but the last character. |
Slice notation also provides for a third argument known as the stride. The stride determines how much to increment the index after reading each element. For example, my_str[0:10:2]
reads every other element between 0 and 10. The stride defaults to 1 if not specified.
|
|