< Prev  |  Home  |  Next >

Python Arrays

When we talked about strings all those days/weeks/months ago (depending on how fast you’re getting through this website), remember how I said to think of a string as characters joined together?

Well, a string is characters joined together (ordered) in an array. This means that we can access certain character positions within the same string variable by using an index number:

Line 3 of the program above prints the character at position 0 in the string called ‘sentence’. Remember that in programming we usually start counting at 0, which is the first character.

I purposefully haven’t told you what line 5 does. Can you work it out? Don’t read this sentence yet, actually go back and have a look at the output to your program.

You should see that it prints a slice of the string from before the character at position 1 (the 2nd character) up to, but not including character at position 3 (the 4th character).

How about this line?

The ‘-1’ is optional, and means ‘go up -1 at a time’, which basically means go backwards. You could enter any number, such as -2, to go backwards 2 at a time, or 3, to print every 3rd character. If a 3rd number isn’t entered, like in line 5 of the program above, then it just goes up 1 at a time (it prints each character).


Can you use the sentence above (or a sentence of your own) to make up words or phrases? You should use character array positions to do this.


I’ve already provided some words in the example above. Go find your own!

Now that you (hopefully) understand arrays, we can use them to provide some nice error checking. Supposing we have a question with a yes/no answer, people could type in any of the following:

yes, Yes, YeS, yep, yes!, no, No, Nope!, etc.

We don’t really want the program to crash, so up until now we have been doing the following:

This helps to ignore the upper case answers (like ‘Yes’), but what about if the user types in ‘Yep’ or ‘yes!’? We could decide that any answer beginning with a ‘y’ means yes, and any answer beginning with ‘n’ means no. This means we only need to look at character 0 of the choice array. like this:

Line 2 means that we are only looking at the first character in the array, turning it into lower case and then seeing if it is a ‘y’. Everything else is OK, because it is ignored.

We could even use a ‘while’ loop, to keep asking for an input until we get a ‘y’ or an ‘n’:

Line 3 of the program checks the first character of the ‘choice’ variable, which doesn’t exist unless we create it, which I’ve done in line 1. Try removing line 1 from your program and you can see the error.

We also get an error if we declare the ‘choice’ variable but set it to be empty:

The reason for this is because otherwise when the program gets to line 3 and looks for the first character in the array (choice[0]), there is nothing there. Actually, it’s not ideal, having to set the variable ‘choice’ to be ‘something’ to make the program work. A better way might be to check that the choice variable isn’t empty, or that it isn’t yet “y” or “n”. We could do it by checking directly:

Or even by checking the length of the variable:

< Prev  |  Home  |  Next >