Login

Python: More Fun with Strings

In our last article we left off discussing ways to manipulate strings in Python, like concatenating, repeating, escape characters, and so forth. In this episode we will pick up where we left off and learn some more ways to work with strings, starting with the comparison method.

Python Tells it Straight: Size Matters

It’s true; Python is a size queen. It’s obsessed with comparing strings, numbers, you name it. It may not make a lot of sense, though, as to how Python sees the value of different strings (uppercase Z is less than lowercase A).

To compare the size of strings, we use the < and > comparison operators, like so:

(type each of these lines at the command prompt)

‘A’ < ‘x’

‘X’ > ‘x’

‘1’ > ‘z’

‘$’ > ‘b’

‘$’ < ‘#’

It should return a True or False, depending upon whether or not the comparison is…well, true or false.

You can also try these…

‘apples’ > ‘Apples’

‘Fred’ > ‘fReD’

‘11111’ > ‘22222’

And of course you can use Python to compare movies as well:

‘Star Trek’ > ‘Star Wars’

‘The Matrix’ > ‘Star Wars’

‘The Matrix’ > ‘Any Movie’

See? Python sure does know its movies…

The rules for which strings are bigger is like so:

Letters at the start of the alphabet are smaller than those at the end

Capital letters are smaller than lowercase letters

Numbers are smaller than letters

Punctuation marks (aside from curly braces, pipe characters, and the tilde) are smaller than numbers and letters.

{mospagebreak title=Working with String Methods}

There are many built-in string methods in Python. We will cover a few of them here, and all of them in a future article.

A method saves the programmer from having to program the same code over and over again. Instead of typing a bunch of code to say, count the number of a’s in a string, all the programmer has to do is call the appropriate method. In the following example, we are going to find out how many times each vowel appears in a string. Type the following into your python shell command prompt:

book=’It was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, Britney Spears was busy being a bad mother…’

book.count(‘a’)

book.count(‘e’)

book.count(‘i’)

book.count(‘o’)

book.count(‘u’)

book.count(‘y’)

The method will count each of the vowels and return a result, like the one below:

>>> book.count(‘a’)

10

>>> book.count(‘e’)

15

>>> book.count(‘i’)

10

>>> book.count(‘o’)

8

>>> book.count(‘u’)

3

>>> book.count(‘y’)

4

We can also count words within the document, like so:

>>> book.count(‘the’)

5

>>> book.count(‘and’)

2

>>> book.count(‘was’)

2

{mospagebreak title=Capital Idea(s)}

We can also use methods to control the appearance of strings, as in the following examples. In our first code, we will make the first letter of our string uppercase, using the capitalize method():

>>> book.capitalize()

‘It was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, britney spears was busy being a bad mother…’

You will note that nothing really changed since our text was already capitalized to begin with.

In this next example we will use the title method() to capitalize the first letter of every word:

>>> book.title()

‘It Was A Dark And Stormy Night, The Wind Blowing Through The Trees, And Somewhere, Out There In The Far Off Teinkling Of City Lights, Britney Spears Was Busy Being A Bad Mother…’

And to make every letter uppercase, we use the upper method():

>>> book.upper()

‘IT WAS A DARK AND STORMY NIGHT, THE WIND BLOWING THROUGH THE TREES, AND SOMEWHERE, OUT THERE IN THE FAR OFF TEINKLING OF CITY LIGHTS, BRITNEY SPEARS WAS BUSY BEING A BAD MOTHER…’

I know, I know…hold back all your Britney is a Good Person YouTube videos. We’re almost done.

And of course we can also change everything to lowercase, using the lower() method:

>>> book.lower()

‘it was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, britney spears was busy being a bad mother…’

Also, you can switch between upper and lower using the swapcase() method. In or previous example, everything was changed to lower case. We can now use swapcase to switch back to uppercase. If we wanted to switch back to lowercase after that, we could again use switchase:

>>> book.swapcase()

‘iT WAS A DARK AND STORMY NIGHT, THE WIND BLOWING THROUGH THE TREES, AND SOMEWHERE, OUT THERE IN THE FAR OFF TEINKLING OF CITY LIGHTS, bRITNEY sPEARS WAS BUSY BEING A BAD MOTHER…’

As I said before, these are only a few of the string methods available; we will discuss the rest at a later date.

{mospagebreak title=Putting Our Strings to the Test}

Sometimes it is necessary to test the data in a string. Maybe we want to know if it contains a specific letter or word or sentence, or any text at all. We can perform a true/false boolean test like so:

>>> cartoon=’The Smurfs rock.’

>>> ‘snork’ in cartoon

False

>>> ‘smurf’ in cartoon

False

>>> ‘smurfs’ in cartoon

False

>>> ‘Smurfs’ in cartoon

True

>>> ‘Smurf’ in cartoon

True

>>> ‘rock’ in cartoon

True

>>> ‘The Smurfs’ in cartoon

True

>>> ‘The Smurfs suck’ in cartoon

False

>>> ‘The Smurfs rock’ in cartoon

True

In the above example we assign the value “The Smurfs rock.” to the variable cartoon. We then run a number of tests to see if certain words and eventually sentences appear in the cartoon variable. You will note that the method is case sensitive (it won’t find ‘smurfs’ but it will find ‘Smurfs’).

You can also use "not in," to see if text is not in the string, like this:

>>> ‘Snorks’ not in cartoon

True

Another method of testing whether some text is in a string is to use the find() method, which returns the position of the text in the string:

>>> cartoon.find(‘Smurfs’)

4

>>> cartoon.find(‘rock’)

11

>>> cartoon.find(‘boo’)

-1

As you can see, the word ‘Smurfs’ begins at the fourth character, and ‘rock’ begins at the eleventh character (spaces count as one character). Since the text ‘boo’ is not in our string, the program returns the value -1.

{mospagebreak title=Converting Data and Sorting}

If you have an integer that you would like to convert to a string, it is possible to do so using the str() function:

>>> str(99999)

‘99999’

To sort, we use the sort() method. Keep in mind that uppercase and lowercase sort differently, so you may wish to convert everything to lowercase or uppercase first. Here is how you sort a list:

In addition to all the wonderful things that Python does to text, it also slices, it dices, it makes julienne fries! And if you act now, it even indexes.

Remember that in Python, a string is basically an array. That is, each character has an index number, starting with zero. If I have a string with some names in it, and I know that say, the name I am looking for starts at index 0 and ends at index 10, I can return that portion of the string like so:

>>> coolnames=’Max Powers, Rod Thickington, Major Whoopass’

>>> coolnames[0:10]

‘Max Powers’

I could of course leave off the 0 (or the starting index) if I wanted to and achieve the same result:

>>> coolnames[:10]

‘Max Powers’

Say I just wanted the power part of the name; I could grab it like so:

>>> coolnames[4:10]

‘Powers’

This is known as slicing. Indexing works in a similar way, only one character at a time:

>>> coolnames[4]

‘P’

We can also use slicing to count backwards, as in the following:

>>> coolnames[-4]

‘p’

>>> coolnames[-1]

‘s’

>>> coolnames[-8]

‘W’

Or…

>>> coolnames[-8:-1]

‘Whoopas’

Note that in the above method, the large number must come first.

We can also use this to write our text backwards if we want to:

>>> coolnames[::-1]

‘ssapoohW rojaM ,notgnikcihT doR ,srewoP xaM’

Well, that’s all the time we have left at the moment. In our next article we will continue our discussion of Python String manipulation and maybe, just maybe, cover the rest of the string methods. So stay tuned!