String Manipulation and File Handling
Aligned to the OCR J277 specification
- Level
- Intermediate
- Reading time
- 6 min
- Published
- 11 June 2026
- Updated
- 1 July 2026
On this page
Key takeaways
- String indices start at 0, so the first character is at index 0 and the last valid index is len(string) - 1.
- Slicing uses string[start:end] where start is inclusive and end is exclusive, so "PYTHON"[0:3] gives "PYT" not "PYTH".
- To concatenate a number with a string you must first cast it to a string using str(); concatenating directly causes a TypeError.
- Opening a file in write mode ("w") deletes all existing content; use append mode ("a") to add data without losing what is already there.
- Always close a file after use with file.close() to ensure data is flushed to disk and the file is not locked.
Worth saving these ideas?
Turn what you've read into instant revision cards. Free to get started.
Key terms
- String
- An ordered sequence of characters, each at a numbered index starting at 0.
- Index
- The numbered position of a character in a string, starting at 0 for the first character.
- Concatenation
- Joining two or more strings together using the + operator to produce a new, longer string.
- Slicing
- Extracting a portion of a string using the syntax string[start:end], where start is inclusive and end is exclusive.
- len()
- A Python function that returns the total number of characters in a string.
- Immutable
- Describes strings in Python - characters cannot be changed in place; modifying a string means creating a new one.
- File handling
- The ability for a program to open, read from, write to, and close files stored on disk, allowing data to persist after the program ends.
- Open mode
- The mode in which a file is opened: "r" for reading, "w" for writing (overwrites), "a" for appending (adds to end).
- readline()
- A Python file method that reads one line at a time from an open file; returns an empty string when the end of the file is reached.
Frequently asked questions
The end index in Python slicing is exclusive, meaning the character at that position is not included. To get the first 4 characters of "PYTHON" use [0:4], which gives characters at indices 0, 1, 2, 3.
Opening a file in write mode ("w") overwrites all existing content and starts fresh. Append mode ("a") adds new data to the end of the file without deleting what is already there.
You must convert the number to a string first using str(). For example, "Score: " + str(87) works correctly. Writing "Score: " + 87 causes a TypeError because you cannot concatenate a string and an integer directly.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Data Types and Casting
Arrays and Records
Related lessons
7 min
6 min
6 min
9 min