Reading Input

name = input("What is your name? ")
print(f"Hello, {name}!")

input() always returns a string. To work with numbers, convert:

age = int(input("Age: "))
price = float(input("Price: "))

print() Basics

print("Hello")
print("A", "B", "C")             # A B C
print("A", "B", sep="-")         # A-B
print("one", end=" ")
print("line")                    # one line

Formatting with f-strings

name = "Ming"
score = 87.5
print(f"{name} scored {score:.1f}")  # Ming scored 87.5

Writing to Standard Error

import sys
print("Error!", file=sys.stderr)

Reading Multiple Values on One Line

a, b = input("Enter two numbers: ").split()
a, b = int(a), int(b)
print(a + b)
๐Ÿ’ก Output format is part of the marking scheme. Match the expected spacing, punctuation, and newlines exactly.

Practise this on PyForm โ€” free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm โ†’