Lesson guide
What this Python exercise practices
Use Escape Characters is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
Summary
Create strings that include newlines, tabs, and quotes using escape sequences.
Problem statement
Write a function format_poem(title, line) that returns a two-line string where the first line is the title, and the second line is the provided line preceded by a single tab character. The exact returned string should be: {title}\n\t{line} Do not print; return the string. Preserve any characters in title and line (including quotes and backslashes).
Task
Learn and practice escape sequences like \n (newline) and \t (tab) to format multi-line strings.
Examples
Simple poem
Input
format_poem('Ode', 'To Joy')
Output
Ode\n\tTo Joy
Explanation
The function returns a string with a newline and a tab before the line.
Input format
Two strings: title and line.
Output format
A single string containing one newline and a tab as described.
Constraints
Return a string. Do not add extra spaces or lines. Preserve characters in inputs.
Samples
Sample input 0
format_poem('Title', '')
Sample output 0
Title\n\t
Explanation 0
An empty second line still gets the tab after the newline.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.