Saturday, November 12, 2011

String operations - Part 2 of 3

I introduced Python strings in an earlier post. A string is a sequence of characters.


In the example above, "a", "b" and "c" are strings. "a" contains the value "jack"

You can extract each character in the string using the [] operator. The first item is at an index value of zero (0). The letter "j" in the string "jack" is at index value zero (0).


The word "jack" has four (4) letters. "j", "a", "c" and "k". The first letter is in position zero (0), the second at position 1, the third at position 2 and the fourth letter is at position 3. When we try to access a letter beyond the length of the string, we get an error message.

The Python len() function will tell you the length of a string. The last character of a string is at the position indicated by len() minus one.


We can iterate through this string, using the for loop and print out each letter.


Notice the comma at the end of the print statement so that we keep the letters on the same line. So that Python does not print the carriage return (or newline).

String slices

Just as you can extract a single character from a string by specifying its index, you can also slice out a sequence of characters by specifying two indices. The starting index and the index after the last character you want.

For example in "jack" if you want to extract "ja" you'd specify zero (0) as the first index and 2 (the index of "c") as the index after the last one. This is counter intuitive to most other programming languages where the second index would be the index of the last character you want. In Python, the second index is the index of the character you don't want.


In the example above, the letter "o" is at index zero (0). The letter "a" is at position 3. So we take everything from the letter "o" and stop before we get to "a". So we get "ont".

The second print statement has a[0:4]. Once again, the letter "o" is at position zero (0). The letter "r" is at position 4. So we'll take everything from the letter "o" and stop before we get to "r". We get "onta".

The third print statement has a[1:3]. The letter "n" is in position 1. The letter "a" is in position 3. So we'll take everything from "n" and stop before we get to "a". We get "nt"

Strings are also numbered from the end. The character at the last position is at index -1. The way to remember this is to think of the fact that index zero (0) is already taken by the first character. So numbering from the end of the string starts at -1. The second-last character is at index -2. And so on.

Each character therefore has two indexes. One index defines its position from the front of the string and a second index that defines its position from the back.


So now its easy to print a string backwards using a loop.


In the loop above, we iterate backwards from -1, down to -4 printing each character in the string.

Slices using negative indices

Slices are always taken forward. You specify a starting index, a second index to stop the slice. The character at the second index is not included.

You can therefore slice using negative indices as long as you remember this. Here are the same three slices we performed on the string "ontario" using negative slices.


We're using the same slice positions, but this time from the back of the string. To get the slice "ont" in the first example we used positions 0:3. In this example we used positions -7:-4. They represent the same characters. The "o" is represented by zero (0), or -7.

One last word about slices. You can omit any of the indices. If you omit the first index, it's assumed that you want to slice from the beginning of the string. If you omit the last one, it's assumed that you want to slice to the end of the string.


No comments:

Post a Comment