Basic String Handling

Searching a variable for a specific word

You can use an if statement to check if a certain word appears in a variable. For example:


If you want to check for more than one word in the same variable and give the same response you would write or between each string as shown above (remember to add the in [variable name] after each alternative string).

Looking at each letter in a variable

We can use a for loop to examine each letter within a string variable, by using the letter placeholder (instead of the usual generic i). We could then use a print command to print each letter individually in a variable, for example:


This would, when run, give the following output:


You can use an if statement within the for loop to program something for a specific purpose:



Counting a specific value

The easiest way to count how many times a certain value appears within a variable is to use the .count() command. Place this command directly after the variable that you wish to search through and, in the the brackets, write the value you want to count. It is important to note that, just like when using an input statement or calculation line, you must save your calculation into a variable. For example, counting the word ‘cow’ in a variable named ‘sentence’ would require:

cow_count = sentence.count(“cow”)

Another example of using the .count() string handling command, for counting the number of e’s in a sentence:


A possible output could be:


Finding the length of a variable

Just like when we wanted to find the length of a list in the last section, we use the len command to find how many characters are in a variable. We also need to save our calculation into a variable, for example:

length = len(sentence)

Why would we need to find the length of a variable? A common reason would be as part of validation – to ensure that an entry is of a suitable length. For example, in checking a password is more than 8 characters we would code:


Entering a password that is too short, such as “Guitar” would give the following output:


Determining the start or end value of  a string

If for some reason you want to determine if the first character in a variable is a specific value then you can use the .startswith() command (remember to save the calculation into a variable). For example:

start = sentence.startswith(“A”)

If the sentence above does start with the letter A then the Boolean value True will be saved as start, else if the variable begins with any other character then start will be saved as False instead. If we were creating a program for a library sorter and we wanted to check if a book reference began with “Geo” then we would use the following code:


Above we have used an if statement to see if the startswith value is True or False and printed an appropriate comment for either scenario. Careful not to put the True value in speech marks as it won’t work. If we ran the above code, two different outcomes could be:


Similarly, you can use .endswith() to check the last characters of a variable.

Reversing a String

To reverse a string you write the variable name and then use square brackets to move one character at a time backwards. The first two colons are left empty as they denote where to start and end from (which will usually be the start and end and don’t need to be changed) Therefore the -1 states that it will reverse from the end to the start:


Printing Parts of a String

You may want to print just part of a string or variable using square brackets. You can also use len to work out the length and work back, if you want to display the last characters:


For example:


Adding Together Numbers in an Integer

One method of adding together values within one integer is to convert it to a string using str() and then define which values are to be printed:


This would print 6 because the 4th and 2nd numbers are 4 and 2 (remember Python starts counting at 0):


String Handling not working? Tick off these common errors:

< Back to Section Nine