Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you manipulate strings in Python: length, position, substrings and case conversion?

Write programs that manipulate strings (length, position, substrings, case conversion).

A focused answer to Edexcel GCSE Computer Science 6.3.3, covering string manipulation in Python: finding length, accessing characters by position, extracting substrings, and converting case.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Strings and length
  3. Position (indexing)
  4. Substrings (slicing)
  5. Case conversion
  6. Combining string operations
  7. Try this

What this dot point is asking

Edexcel wants you to write programs that manipulate strings: find a string's length, access characters by position (index), extract substrings (parts of a string), and convert case (upper and lower).

Strings and length

word = "hello"
print(len(word))   # 5

Length is the starting point for much string work: a loop that processes each character runs from index 0 to len(word) - 1, and a length check is a common validation (for example a password must be at least 8 characters).

Position (indexing)

word = "hello"
print(word[0])            # h
print(word[len(word)-1])  # o (the last character)

Indexing from 0 is the source of many off-by-one errors, so remember the first character is index 0 and the last is len - 1. Accessing an index beyond the end causes a runtime error.

Substrings (slicing)

s = "computer"
print(s[0:3])   # com  (indices 0, 1, 2)
print(s[3:])    # puter (index 3 to the end)
print(s[:3])    # com  (start omitted)

The "up to but not including the end index" rule is the key point: s[0:3] gives three characters (indices 0, 1 and 2), not four. Slicing is how you take the first few characters (for a username), a fixed field, or any portion of text.

Case conversion

name = "Sam"
print(name.upper())   # SAM
print(name.lower())   # sam

A common use is making input checks ignore case: converting both the user's input and the stored value to the same case with .lower() before comparing means "YES", "Yes" and "yes" all match. Case conversion is also used to format usernames or display text consistently.

Combining string operations

Real string tasks combine these operations: you might find the length to validate, slice out a field, convert its case, and join it with other text. Concatenation with + builds new strings, and a loop over the indices (using len) processes each character. The exam often asks you to write a small function that transforms a string this way, returning the result, so practise combining len, indexing, slicing, upper/lower and +.

Try this

Q1. State what len("data") returns. [1 mark]

  • Cue. 4.

Q2. State the substring returned by "network"[0:3]. [1 mark]

  • Cue. "net" (indices 0, 1 and 2).

Exam-style practice questions

Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Edexcel 20225 marksA program reads a word and outputs its length and its first three characters in uppercase. Write the Python program.
Show worked answer →

Use len() for length, slicing for the first three characters, and upper() for case.

word = input("Enter a word: ")
print(len(word))
print(word[0:3].upper())

For "computer" this prints 8, then "COM". Markers reward reading the input, using len() for the length, slicing word[0:3] (or [:3]) for the first three characters, and upper() to convert case, with correct output. Mishandling the slice bounds is the usual error.

Edexcel 20214 marksA username is formed from the first 3 letters of the surname (in lowercase) followed by a 2-digit ID. Write a Python function make_username(surname, id) that returns the username. Assume id is a 2-character string.
Show worked answer →

Slice the first three characters of the surname, lower the case, and join with the id.

def make_username(surname, id):
    return surname[0:3].lower() + id

For make_username("Smith", "07") this returns "smi07". Markers reward slicing the first three characters, converting to lowercase, concatenating with the id, and returning the result (not printing it, since a function is asked for).

Related dot points

Sources & how we know this