GCSE · Computer Science · AQA · Spec 8525

String position

Ask a computer where the 's' is in 'mississippi' and its answer surprises almost everyone. Make your guess, then watch it search.

Computer Science · Strings

Where is the first 's' in 'mississippi'?

The program asks for the position of 's'. Step through the search one character at a time and watch where it stops.

Before you press Next, make a guess: what number will this program output?

1 word ← 'mississippi'
2 where ← POSITION(word, 's')
3 OUTPUT where
indexcharactermatch?where

Output

 

Step 1: word holds 11 characters. Each one sits in a numbered slot, and the very first slot is number 0.

1 / 6

Press Next to move the search along one character. Did your guess match the output?

Step 1 of 6: word holds 11 characters. Each one sits in a numbered slot, and the very first slot is number 0..

Exam line: A position answer is one number: the index of the FIRST match, counting the first character as 0.
Watch out: Don't answer with the 'which letter is it' count. The first slot is numbered 0, so every index is one less than that count.

What do you really think?

What does position give back?

A program runs POSITION('coconut', 'o'). The word 'coconut' has two o's, and the pseudo-code counts from 0.

Which of these is closest to what you think the program gets back?
How sure are you?

Now you try: use the index

One-digit or two-digit hour?

A program stores a time such as '7:45' or '10:05' in the variable t. The hour has one or two digits, and there is always exactly one ':'. The pseudo-code counts from 0. Build the program that outputs 'one-digit hour' or 'two-digit hour'.

  1. Find the colon and keep its index: colon ← POSITION(t, ':')Store it first, so the program can test it on the next lines.
  2. missing step
Which line is step 2?

Your turn: write the code

How long is the name part?

A program stores an email address in the variable email, for example 'sam@mail.com'. The address always contains exactly one '@'. The pseudo-code counts from 0. Write pseudo-code (or code in a language you know) that: • finds the position of the '@' in email and stores it in a variable called atPos • outputs how many characters come before the '@'. [3 marks]

0 words · your answer stays on this page and is not sent anywhere.

Exam line: Store the result of POSITION in a variable first; then the rest of the program can output it, compare it or calculate with it.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Count from 0. Stop at the first match.

What you need to know

  • Position takes a string and a character and gives back a whole number: the index where that character first appears.
  • In the pseudo-code here, and in Python, Java, C# and JavaScript, indexes start at 0. The first character is index 0, the second is index 1, and so on. (A few languages count from 1, so check the language you're using.)
  • The search stops at the first match. Any later copies of the character are ignored.
  • Store the result in a variable, e.g. where ← POSITION(word, 's'), so the rest of the program can use it.

The big picture

Position is a string handling operation. You give it a string and a character, and it gives back a number: the index of the first place that character appears. In the pseudo-code used here, and in languages such as Python, Java, C# and JavaScript, indexes count from 0, so the first character is at index 0. The search stops at the first match, so later copies of the character are ignored. Store the result in a variable and the rest of your program can use it.

Key points

1Position answers 'where?' with one number, the index of the first match.
2Counting from 0 means a character's index is one less than its 'which letter is it' count.
3Later matches never change the answer, because the search has already stopped.
4Once the index is stored in a variable, it can be output, compared in an IF, or used in a calculation.

Worked example

Problem

A program stores code ← 'AB-77-31'. It then runs dash ← POSITION(code, '-') followed by OUTPUT dash + 1. What does the program output?

⚠ Watch out

Answering with the 'which letter is it' count. When counting from 0, the 4th character of a string is at index 3, not 4.

🧠

Memory hook

Finger on the first letter, say ZERO. Slide right, counting, and stop at the very first match.

✓

Check yourself

What does POSITION('cheese', 'e') give back, and why isn't it 3 or 5? (Answer: 2. The search stops at the first e, so the others are never reached.)

Flashcards

(7)
What does the position operation give back?
A whole number: the index where the character first appears in the string.
With zero-based indexing, what is the index of the first character in a string?
Index 0.
A character appears three times in a string. Which one does position report?
The first one. The search stops at the first match, so the later ones are never checked.
With zero-based indexing, the 5th character of a string is at which index?
4. The index is one less than the 'which letter is it' count.
What is POSITION('banana', 'n')?
2 (b is 0, a is 1, n is 2). The n at index 4 is ignored.
Write pseudo-code that stores where 'x' first appears in text, in a variable called pos.
pos ← POSITION(text, 'x')
Why store the result of POSITION in a variable?
So the program can use the number later: output it, compare it in an IF, or calculate with it.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.