Sunday, November 13, 2011

String operations - Part 3 of 3

Python Strings are immutable. Which means that once you create one, you can't change it. In part 2 of our discussion on strings, we saw how to extract a character from a string.


However, it's illegal to try and replace a character inline. The string "a" in the example above cannot be changed.


You get the error message displayed above. That the 'str" object does not support assignment. If you want to change the contents of the string, the only thing to do is create another one. Using slices, you can do the following:


What we did is take the letter "m", add, or concatenate, it to the rest of the original string "a" starting at position 1 ("ack") and then assign it to a new string called "a." Looks like we modified the original string "a" but in effect what Python does is destroy the old string "a" and create a new one. This allows us to do the following:


In each of the assignments above, the original string "a" is destroyed, a new string is created with the characters on the right side, and that's assigned to the variable "a." So, a="jack" is a different string from a="jill".

We've already seen how to find the length of a string using the Python function len().


We can also use the for loop in Python to count the characters.


The string module has a find() function. This function allows you to find a character, or another string, inside a string. In order to use the find() function, we have to import the string module.


Notice how we call the find() function using the string module class identifier. The find() function is called a class function. It requires two arguments; the string that you're searching and the substring to look for.

In our example:

string.find(a, "n")

This instructs the find() function to look for the "n" string inside the "a" string. The "a" variable points to the string "canada". In that string, "n" is at position 2, remembering that the first character is at position 0.

If the find() function does not find the string that we're looking for, it returns the number -1.


See that the value of "i", in the second find() is -1 because the string "canada" does not contain the letter "q" that we're looking for.

The find() function is not limited to looking for simple characters, it can also find entire words.


In the find() function call above, the word "canada" is at position 36 in the string "a". The number 36 is the starting position of the word "canada". It's the position where the "c" in "canada" is.


In the example above, after finding the position of the string "canada" we extracted it. Not a very useful example but illustrative of the fact that you can use an example of this sort to pull out words, based on whitespace.

The string module contains some useful functions that will help when analysing text.


How would you use these? Well, you can use the string.lowercase variable to test if a letter in your particular string is lowercase. Here's an example that counts lowercase and uppercase characters in a string.


We have our string with upper and lowercase characters, "s". We then use a for loop to loop through each character. If its a lowercase character, we increment the value of the lowercount variable. If its an uppercase character, we increment the value of the uppercount variable. Notice how we use the find() function.

string.find(string.lowercase, item)

"item" is the character we're looking for.
string.lowercase is the string we're searching in. string.lowercase contains all the lowercase characters. If we can't find the character, the find() function returns the value -1. Otherwise it returns a value from zero (0) to the length of the string.

There is another, more elegant, way to do something like this. That's using the string "in" operator. The "in" operator checks to see if one string is "in"side another. For example, if the string is "canada" and we issue the following statement:

a = "can" in "canada"

The value of "a" will be True. Because the string "can" is inside "canada"

Here's the counting program, written using the "in" operator.



The string module has a number of interesting functions that you can use to manipulate strings. Functions to modify strings from uppercase to lowercase and vice versa. To split strings at whitespace into words. To convert strings to numbers.

No comments:

Post a Comment