String Manipulation

Working with & manipulating strings

Hopefully you should have an understand of what a string is in relation to programming as been a sequence of characters e.g. "Jeffrey123" or "Jaffa Cake". This lesson will take you through the process of learning more about strings and how to use them in creative ways.

As you work your way through the tutorial you will be creating several different programs, remember to save them all with sensible names and run your code to check it all works.

Lesson Knowledge Descriptors

Got It: I can use a range of methods to manipulate strings in the Python programming language, including searching, slicing and counting.

Nearly There: I can manipulate strings by selecting specific elements of the string to use within a program or display on the screen.

Need Support: I can use strings in a program as a whole sequence of characters.

Slicing Strings

As explained above a string is a sequence of characters, both strings and characters are data types.

Using a technique called slicing you can take a complete string and break it down into individual characters that make up the whole sequence. Each character can be identified by the its numerical position in the sequence, notice that spaces have a position.


Programming Activity 1

The code shown opposite takes a variable called phrase which contains a string and slices it into characters. The program then displays individual characters from the string on the screen.



  1. Copy the code into a new Python file
  2. Add #comments onto each line describing what it is doing.

Programming Activity 2

The code shown opposite takes a variable called phrase which contains a string and calculates the length of the string before displaying that on the screen.

Beware there is an intentional error within the code!



  1. Copy the code into a new Python file
  2. Add a #comment onto line 2 describing what it is doing
  3. Modify line 2 so it puts the length of the string into a sentence such as "The length of the string is 5"
  4. Add to the program so that somebody can type in a sentence and then it tell them the length of it

Programming Activity 3

The code shown opposite takes a variable called phrase which contains a string and counts how many times specific character appear within that string.



  1. Copy the code into a new Python file
  2. Add a #comment onto line 3 describing what it is doing
  3. Why is the 3rd number outputted a 0 when the string clearly contains the letter s? Can you correct this
  4. Modify the code so it puts the count of the character into a sentence such as "There is 3 a's within this string"

Programming Activity 4

The code shown opposite takes a variable called phrase which contains a string and changes the case of the string in several ways.

This is very similar to the first tutorial you worked through looking at basic inputs and outputs where we were changing inputs into uppercase.



  1. Copy the code into a new Python file
  2. Add a #comment onto line 4 & 5 describing what it is doing
  3. Add to the program so that somebody can type in a sentence and then it tell them the relevant information for their sentence.

Programming Activity 5

The code opposites takes a really long string that could potential be awkward to read on the screen of a PC.

Some IDE's will continue on one long line rather than wrapping the text, a simple way to force it do this is to use " " " and then " " " at the of the string also.



  1. Copy the code into a new Python file (Change the words if you want)
  2. Add a #comment describing what it is doing

Finished the tutorial & done the quiz? Why not have a go at a programming challenge, they are a really useful way to build your skills.