Operators and String Manipulation
Operators — The Tools of Calculation
An operator tells Python how to combine or compare values. There are three categories you must know: arithmetic, relational, and logical. Understanding them precisely matters because exam questions often test the output of specific expressions — especially for relational operators where the expected output is True or False, not “yes” or “no”.
Arithmetic Operators
Python supports seven arithmetic operators. Four are familiar: + (addition), - (subtraction), * (multiplication), / (real division — the result can be a decimal). The other three are integer division (//), modulus (%) and exponentiation (**).
print(17 // 5) # Integer division: 3 (5 goes into 17 three full times) print(17 % 5) # Modulus: 2 (17 = 3*5 + 2, remainder is 2) print(2 ** 8) # Exponentiation: 256 (2 to the power of 8) print(7 / 2) # Real division: 3.5
The modulus operator (%) returns the remainder after integer division. A classic use is testing whether a number is odd or even: n % 2 == 0 is True if n is even. It also appears in creating circular patterns, validating credit card numbers, and extracting individual digits.
Relational Operators
Relational operators compare two values and always return a Boolean: True or False. This is a common exam error — students write “yes” or “1” when the correct answer is True.
print(10 == 10) # True print(10 != 5) # True print(3 < 2) # False print(5 >= 5) # True
The six relational operators are: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to). Note that == tests equality; a single = in Python is assignment, not comparison.
This is one of the most common errors in exam answers. If a question asks “what is the output of print(7 > 3)?”, the answer is True (capital T, no quotes). Writing “yes” or “1” is wrong. The result of any comparison in Python is a Boolean value — either True or False.
Logical Operators
Logical operators combine or invert Boolean conditions. Python uses and, or, and not (all lowercase).
age = 17
has_consent = True
if age >= 16 and has_consent:
print("Eligible for trip")
if age < 13 or age > 65:
print("Reduced price ticket")
if not has_consent:
print("Permission required")
and requires all combined conditions to be True. or requires at least one to be True. not inverts: not True gives False. These match the Boolean logic used in search queries, access controls, and input validation throughout real systems.
String Operations
Python has powerful built-in tools for working with strings. The length of a string is found with len(). Indexing retrieves a single character by position, starting at 0. Slicing extracts a section of the string.
name = "Computer" print(len(name)) # 8 print(name[0]) # C (first character, index 0) print(name[3]) # p (index 3) print(name[0:4]) # Comp (index 0 up to but not including 4)
For the string s = "GCSE2026":
len(s)→8s[0]→'G'(first character)s[len(s) - 1]→'6'(last character — the PLS does not include negative indexes, so uselen()to find the last position instead)s[4:8]→'2026'(characters at positions 4, 5, 6, 7)s[0:4]→'GCSE'(positions 0, 1, 2, 3)
Remember: Python counts from 0, and the end index in a slice is excluded. So s[0:4] gives characters at positions 0, 1, 2, 3 — not position 4.
Case Conversion and Concatenation
Python provides two methods for changing a string’s case: .upper() returns a version with all letters capitalised; .lower() returns a version with all letters in lowercase. These are useful when comparing user input regardless of how it was typed.
city = "London"
print(city.upper()) # LONDON
print(city.lower()) # london
# Safe comparison:
response = input("Enter yes or no: ")
if response.lower() == "yes":
print("Confirmed")
Concatenation joins strings together using +. Only strings can be concatenated — trying to add a string and an integer gives a TypeError. Use str() to convert a number to a string first: "Score: " + str(85).
A school system creates a username from the first letter of a student’s first name plus their full surname, all in lowercase:
first_name = input("First name: ")
surname = input("Surname: ")
username = first_name[0].lower() + surname.lower()
print("Username:", username)
If the student enters “Priya” and “Sharma”, the username becomes “psharma”. This uses indexing ([0]), case conversion (.lower()), and concatenation (+) in a single line.