Python string manipulation

Strings are the most-used data type in real Python programs, and every UK GCSE Computer Science specification lists string handling as a required skill. This unit covers indexing (s[0]), slicing (s[0:3]), len(), the common string methods (.upper(), .lower(), .strip(), .split(), .replace(), .find()), concatenation with +, and f-strings for clean formatted output. Every snippet runs in the browser so learners can try edge cases (empty strings, spaces, mixed case) themselves. Perfect for classroom teaching, GCSE and A Level revision, and independent or home-schooled learners.


What you'll learn

  • Index individual characters with s[0], s[1], s[-1]
  • Slice substrings with s[start:end]
  • Use len(s) to measure a string
  • Clean input with .strip(), .lower(), .upper()
  • Split and join strings with .split() and .join()
  • Format output with f-strings

UK GCSE topic coverage

  • Strings as sequences
  • String indexing
  • String length reasoning
  • String `+` and `*`
  • String equality
  • String methods (case)
  • String methods (whitespace)
  • String slicing
  • Applied strings
  • Applied strings + selection
  • String methods (`in`, `.count()`)

Worked example

Validate and format a name from user input

raw = input("Name? ")
name = raw.strip().title()
if len(name) == 0:
    print("You didn't type anything.")
else:
    print(f"Hello, {name}!")

Try a full unit free with a RunPy account

Sign up free and get the whole of Unit 1 (Input, Output and Variables) with checked tasks and instant feedback, plus a selection of the exam-style questions so you can practice on real GCSE-style content before upgrading.


Lessons in this unit

Full interactive lessons with checked tasks are available to signed-in RunPy users. Unit 1 and a selection of exam-style questions are free with a RunPy account.

  1. Strings as sequences of characters
  2. Positive indexing and boundaries
  3. Negative indexing
  4. Using len() and off-by-one reasoning
  5. Concatenation and string repetition
  6. String comparison and case sensitivity
  7. Normalising text with .lower() and .upper()
  8. Cleaning input with .strip()
  9. Slicing basics: extracting substrings
  10. Left and right slice patterns
  11. GCSE pattern: username creation
  12. GCSE pattern: password validation
  13. Checking which characters are in a string

Frequently asked questions

Are Python strings zero-indexed?
Yes. The first character of s is s[0]. Negative indices count from the end, so s[-1] is the last character.
Do string methods change the original string?
No. Strings are immutable, so methods like .upper() and .strip() return a new string. If you want to keep it, assign the result: name = name.strip().
What's an f-string?
An f-string is a formatted string literal, prefixed with f. Inside braces you can drop variables and expressions directly, e.g. f"Hello, {name}!". Cleaner than concatenation with +.