# Logical operators in conditionsage = 17has_licence = Falseif age >= 17and has_licence:print("Can drive!")else:print("Cannot drive") # prints this (has_licence is False)ifnot has_licence:print("You need a licence!")
Truth Tables
A
B
A and B
A or B
not A
True
True
True
True
False
True
False
False
True
False
False
True
False
True
True
False
False
False
False
True
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 characterprint(s[-1]) # 'o' — last character (negative indexing)print(s[1:4]) # 'ell' — characters at index 1,2,3print(s[:3]) # 'Hel' — first 3 charactersprint(s[2:]) # 'llo' — from index 2 to end
String Methods
Method
What it does
Example
Result
.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 membershipword = "Python3"print(word.isalpha()) # False — contains digitprint(word.isdigit()) # False — contains lettersprint(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
✅ Notes completed!
▶
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]
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!
Term
Definition
🎯
Mini Test — Logical Ops & Strings
Timed exam-style test — 10 minutes.
⏱10:00
Section A — Multiple Choice [5 marks]
Q1What is the result of: True and False?[1]
Q2s = "Python"; what does s[1:4] return?[1]
Q3Which Python keyword is equivalent to logical OR?[1]
Q4What does "hello".upper() return?[1]
Q5What is the output of: print(len("CSZone"))?[1]
Section B — Short Answer
Q6Complete this truth table for: A and B, A or B, not A (when A=False, B=True).[3]
Mark schemeA and B: False (False and True = False) [1]; A or B: True (False or True = True) [1]; not A: True (not False = True) [1].
Q7Write Python code to extract just the first name from a full name stored as "John Smith" (split on the space and get the first word).[2]
Mark schemefull_name = "John Smith"; parts = full_name.split() [1]; first_name = parts[0] [1]. (Accept: first_name = "John Smith".split()[0] as one line.)