File Operations – Write
.write() writes a string to a file opened in write or append mode
What it does: \n adds a newline so the next write starts on a new line. writelines() writes each string from the list — doesn't add newlines automatically. write() sends text to the file — print() sends it to the screen. Unlike print(), write() is literal — add '\n' to end each line. 'w' truncates the file to zero length before writing — use 'a' to preserve content. 'a' (append) keeps existing content and adds new text after it. write() returns the count of characters written — 'Hi' has 2 characters. Each write() call appends to the file — include '\n' to separate lines.
Common mistakes: write() only accepts strings: convert number with str(). Can't write in 'r' mode: use 'w' or 'a'.
Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
.write(string)Fill in the Blanks
Spot the Error
Create a free class, add this module to a journey, and track who's stuck.
Create a free teacher account →No credit card. No setup fee. Make a class and invite students in under three minutes.
Practice this topic interactively, track progress, and unlock badges.
Start Learning Free →