🔒

Unlock Pro

Subscribe to access all 59 Edexcel 1CP2 lessons.

£7.99/month
or £59/year
🐍 Paper 2 · Topic 6: Programming
6.2b Logical Operators & String Operations
Edexcel 1CP2 · GCSE Computer Science · ~12 min read · 🔒 Pro
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

Logical Operators

Logical operators combine or modify Boolean (True/False) conditions. Python uses three: and, or, not.

OperatorMeaningExampleResult
andTrue if BOTH conditions are True5 > 3 and 10 > 7True
orTrue if AT LEAST ONE condition is True5 > 3 or 10 < 7True
notInverts a Boolean (True→False, False→True)not TrueFalse
# Logical operators in conditions age = 17 has_licence = False if age >= 17 and has_licence: print("Can drive!") else: print("Cannot drive") # prints this (has_licence is False) if not has_licence: print("You need a licence!")

Truth Tables

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

String Operations

Strings are sequences of characters. Python has many built-in string operations:

Concatenation

first = "Hello" second = "World" print(first + " " + second) # Hello World

String Length

word = "Python" print(len(word)) # 6

Indexing and Slicing

s = "Hello" print(s[0]) # 'H' — first character (index 0) print(s[4]) # 'o' — last character print(s[-1]) # 'o' — last character (negative indexing) print(s[1:4]) # 'ell' — characters at index 1,2,3 print(s[:3]) # 'Hel' — first 3 characters print(s[2:]) # 'llo' — from index 2 to end

String Methods

MethodWhat it doesExampleResult
.upper()Converts to uppercase"hello".upper()"HELLO"
.lower()Converts to lowercase"HELLO".lower()"hello"
.strip()Removes whitespace from both ends" hi ".strip()"hi"
.replace(a,b)Replaces all occurrences of a with b"cat".replace("c","b")"bat"
.split()Splits string into a list"a b c".split()['a','b','c']
.find(s)Returns index of first occurrence"hello".find("l")2

Checking string content

if "a" in "apple": print("Found it!") # prints — 'in' tests membership word = "Python3" print(word.isalpha()) # False — contains digit print(word.isdigit()) # False — contains letters print(word.isalnum()) # True — letters and numbers only
Exam tip: String indexing starts at 0 in Python. "Hello"[0] = 'H', "Hello"[4] = 'o'. Slicing [start:stop] includes start but NOT stop. These are very common in Edexcel Paper 2 practical tasks.
⚠️ Common Mistakes
  • Off-by-one errors in indexing — string[0] is the first character, not string[1]
  • Slicing [1:4] gives characters at positions 1, 2, 3 (NOT 4) — stop is exclusive
  • Using and/or instead of &&/|| (Python uses words, not symbols)
  • Strings are immutable — you cannot change a single character; you must create a new string
  • Forgetting that .upper()/.lower() return a new string — they don't modify the original
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — 6.2b Logical Operators & Strings

8 Edexcel-style questions · instantly marked

Q1State the result of each logical expression: (a) True and False (b) True or False (c) not True (d) False or False[4]
✅ Mark scheme
(a) False [1]; (b) True [1]; (c) False [1]; (d) False [1].
Q2What is the output of: s = "Computer"; print(s[0], s[3], s[-1])?[3]
✅ Mark scheme
C [1]; p [1]; r [1]. Computer: C(0) o(1) m(2) p(3) u(4) t(5) e(6) r(7). s[-1] is the last character.
Q3Write Python code using logical operators to check if a number n is between 1 and 100 (inclusive). Print "Valid" or "Invalid".[3]
✅ Mark scheme
if n >= 1 and n <= 100: [1] (accept 1 <= n <= 100 — Python allows chained comparison); print("Valid") [1]; else: print("Invalid") [1].
Q4What does the slice s[2:5] return if s = "abcdefg"?[2]
✅ Mark scheme
"cde" [2] — s[2]='c', s[3]='d', s[4]='e'. Slicing [2:5] includes indices 2, 3, 4 but NOT 5 (stop is exclusive). s[5]='f' is excluded.
Q5Write Python code that asks for a password, converts it to uppercase, and checks if it equals "SECRET". Print appropriate messages.[4]
✅ Mark scheme
password = input("Enter password: ") [1]; password = password.upper() or use password.upper() in condition [1]; if password == "SECRET": [1]; print("Access granted") / else: print("Wrong password") [1].
Q6Explain the difference between 'and' and 'or' in Python. Give an example of when you would use each.[4]
✅ Mark scheme
and: both conditions must be True for the result to be True [1]; e.g. checking age >= 18 and has_id to verify someone can buy alcohol [1]; or: at least one condition must be True for the result to be True [1]; e.g. if answer == "yes" or answer == "y": to accept either form of input [1].
Q7What is the output of: word = "hello world"; print(len(word)); print(word.replace("l", "L"))?[2]
✅ Mark scheme
11 [1] (space counts as a character); heLLo worLd [1] (all three 'l's replaced with 'L'). Note: len() counts the space too.
Q8A program asks for a username. Validate that it is between 3 and 12 characters long and contains only letters and numbers. Write the Python condition for this.[3]
✅ Mark scheme
username = input("Username: "); if len(username) >= 3 and len(username) <= 12 and username.isalnum(): [3 — 1 for each condition: length lower bound, length upper bound, isalnum check]. Could also use 3 <= len(username) <= 12.
Topic Quiz
Q 1 of 15
You scored
out of 15
Click to reveal definition
🎉
Session complete!
TermDefinition
🎯

Mini Test — Logical Ops & Strings

Timed exam-style test — 10 minutes.

← 6.2a Arithmetic OperatorsTopic 6 · PythonNext: 6.3a Lists →