Python 9a - String Handling

What is String Handling?

String handling refers to the manipulation of a string variable, typical uses include:

  • Checking the length of a variable.

  • Searching a variable for a certain phrase.

  • Checking the number of times a specific character or word is used.

In the real-world, string handling is used to examine passwords and to ensure that they are of an acceptable strength (by checking that they include a mixture of capital letters, lowercase letters and symbols for example).

Counting in a String

The easiest way to count how many times a certain value appears within a variable is to use the .count() command.


It is important to note that, just like when using an input statement or calculation line, you must save the calculation into a variable.


An example, for counting the number of e’s in a sentence, is below:

Practice Task 1

Create a program that counts how many instances of the letter a have been entered in a sentence.

Example solution:

Finding the Length of a String

Just like when we wanted to find the length of a list, we use the len command to see how many characters are in a string.


We also need to save the calculation into a variable.

A common reason for finding the length is as part of validation, for example, checking a password is more than 8 characters we would code:

Practice Task 2

Create a program that asks for a name.

Check that the name is between 4 and 10 characters.

Print appropriate messages if it is within this range and if it isn't.

Example solution:

Checking the Start / End of a String

To determine if the first character in a string is a specific value 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 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 string.

Practice Task 3

Ask a user to enter their name.

If their name starts with the same letter as your name then print an appropriate message.

Else, print a different message.

Example solution:

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 don’t need to be changed).

Therefore the -1 states that it will reverse from the end to the start:

Ask the user to enter a random sentence.

Print the sentence in reverse.

Example solution:

Practice Task 4

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:

Practice Task 5

Ask the user to input a long word.

Output the middle character of the word.

Example solution:

© CSNewbs 2021

The written, video and visual content of CSNewbs is protected by copyright. © 2020