All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

String handling: length, indexing, examining and manipulating

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Strings appear in almost every Unit 2 question: cleaning input, checking passwords, building output. A string is an immutable sequence of characters.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Length, indexing and slicing (spec 8.2.2)

len(s) gives the number of characters. s[i] is one character (from index 0); s[-1] is the last. s[a:b] is a slice from a up to, not including, b; s[::-1] reverses.

word = "Computer"
print(len(word), word[0], word[-1])
print(word[3:6], word[:4], word[4:])
print(word[::-1])

Output:

8 C r
put Comp uter
retupmoC

Strings are immutable: word[0] = "c" raises TypeError. Build a new string instead.

Examining strings

Methods that test a string return True or False; find() and index() locate a substring.

code = "AB12"
print(code.isalnum(), code.isalpha(), code.isdigit())
print("abc".islower(), "ABC".isupper(), "   ".isspace())
print("banana".find("an"), "banana".find("x"))
print("ss" in "mississippi")

Output:

True False False
True True True
1 -1
True

find() returns -1 if not found; index() raises ValueError instead.

Manipulating strings

These methods return a new string:

raw = "  hello WORLD  "
print(raw.strip())
print(raw.strip().lower(), raw.strip().upper())
print("ada lovelace".title(), "python".capitalize())
print("a,b,,c".split(","))
print("one two  three".split())
print("-" * 10)

Output:

hello WORLD
hello world HELLO WORLD
Ada Lovelace Python
['a', 'b', '', 'c']
['one', 'two', 'three']
----------

To build a cleaned string, loop over the characters and concatenate the ones you want.

Building strings in a loop

A common exam task: keep only letters, in lower case, then test for a palindrome.

text = "Never odd, or even!"
clean = ""
for ch in text.lower():
    if ch.isalpha():
        clean = clean + ch
print(clean)
print(clean == clean[::-1])

Output:

neveroddoreven
True

Worked example

Count the vowels in "Programming Language".

  1. Loop over text.lower() and test each character with: if ch in "aeiou"
  2. Vowels found: o, a, i, a, u, a, e
  3. Count: 7

Answer: 7 vowels

Key idea: len(), indexing from 0 and slicing [a:b] (b excluded) work on strings; is...() methods examine, strip/lower/upper/split build new strings; strings are immutable.

Check yourself

  1. What is displayed when this code runs? ```python s = "Algorithm" print(s[2:5]) ```
    Show answer

    gor. Indexes 2, 3 and 4 are g, o, r: gor.

  2. What is displayed when this code runs? ```python s = "keyboard" print(s[-3:]) ```
    Show answer

    ard. The last three characters: ard.

  3. What is displayed when this code runs? ```python s = " data " print(len(s), len(s.strip())) ```
    Show answer

    8 4. The original has 8 characters including 4 spaces; strip() removes them leaving 4: 8 4.

  4. What is displayed when this code runs? ```python email = "sam.lee@school.org" at = email.find("@") print(email[at + 1:]) ```
    Show answer

    school.org. find() returns the index of @, so the slice after it is the domain: school.org.

  5. What is displayed when this code runs? ```python line = "Kim,17,Maths" parts = line.split(",") print(parts[1] + parts[1]) ```
    • 1717
    • 34
    • 17
    • Kim17
    Show answer

    1717. split gives strings, so parts[1] is "17" and + joins: 1717.

  6. What is displayed when this code runs? (The two results are separated by a space.) ```python print("hello world".title(), "hello world".capitalize()) ```
    • HELLO WORLD Hello world
    • Hello world Hello World
    • Hello World Hello world
    • hello World Hello world
    Show answer

    Hello World Hello world. title() capitalises each word; capitalize() only the first character: Hello World Hello world.

  7. Which exception is raised when this code runs? ```python name = "jon" name[0] = "J" ```
    • TypeError
    • IndexError
    • ValueError
    • NameError
    Show answer

    TypeError. Strings are immutable, so item assignment raises TypeError. Use name = "J" + name[1:] instead.

  8. This code should keep only the digits from a phone number. Which line completes it? ```python raw = "(0161) 496-0000" digits = "" for ch in raw: __________ digits = digits + ch print(digits) ```
    • `if ch.isalpha():`
    • `if ch.isdigit():`
    • `if ch.isalnum() == False:`
    • `if ch != " ":`
    Show answer

    `if ch.isdigit():`. isdigit() is True only for the characters 0 to 9.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.