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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- Write programs that make appropriate use of primitive data types (integer, real, Boolean, char) and one- and two-dimensional structured data types (string, array, record), and that make appropriate use of variables and constants.
A focused answer to Edexcel GCSE Computer Science 6.3.1 and 6.3.2, covering the primitive data types (integer, real, Boolean, char), structured types (string, array, record) in one and two dimensions, and using variables and constants.
- Identify the structural components of programs (constants, variables, initialisation, assignment, sequence, selection, repetition, iteration, data structures, subprograms, parameters, input/output) and write programs that use sequencing, selection, repetition (count-controlled, condition-controlled) and iteration with single entry and exit points.
A focused answer to Edexcel GCSE Computer Science 6.2, covering the structural components of programs and writing Python programs that use sequence, selection, count-controlled and condition-controlled repetition, and iteration.
- Write programs that accept and respond appropriately to user input, and that read from and write to comma separated value text files.
A focused answer to Edexcel GCSE Computer Science 6.4.1 and 6.4.2, covering accepting and responding to user input, and reading from and writing to comma separated value (CSV) text files in Python.
- Understand the need for and write programs that implement validation (length check, presence check, range check, pattern check) and authentication (ID and password, lookup).
A focused answer to Edexcel GCSE Computer Science 6.4.3 and 6.4.4, covering the need for validation, the length, presence, range and pattern checks, and authentication by ID and password lookup in Python.
- Understand how computers encode characters using 7-bit ASCII.
A focused answer to Edexcel GCSE Computer Science 2.2.1, covering how computers encode characters using a character set, the 7-bit ASCII system, its 128 codes, and the ordering of letters and digits.
Sources & how we know this
- Pearson Edexcel GCSE (9-1) Computer Science (1CP2) specification — Pearson (2020)
- Pearson Edexcel GCSE Computer Science Programming Language Subset (Python) — Pearson (2020)