Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Escape characters in output
📖 Scenario: Imagine you are writing a message that includes special characters like quotes and new lines. You want to show this message exactly as it is, including the special characters, in your program's output.
🎯 Goal: You will create a string with escape characters and print it so the output shows the special characters correctly.
📋 What You'll Learn
Create a string variable with escape characters for quotes and new lines
Use the correct escape sequences like \" for double quotes and \n for new lines
Print the string so the output matches the expected format
💡 Why This Matters
🌍 Real World
Escape characters are used when you want to include special symbols or formatting inside text, like quotes, new lines, or tabs, which is common in writing messages, logs, or formatted output.
💼 Career
Understanding escape characters helps in programming tasks like generating reports, handling file paths, or preparing text for display or storage, which are common in many software development jobs.
Progress0 / 4 steps
1
Create a string with quotes and new lines
Create a string variable called message with the exact value: She said, \"Hello!\"\nWelcome to the escape characters lesson.
Python
Hint
Use \" to include double quotes inside the string and \n for a new line.
2
Add a second line with a tab escape character
Add to the message string a new line with a tab character and the text: \tThis line is indented. Use the plus sign + to join the strings.
Python
Hint
Use \n for new line and \t for tab inside the string you add.
3
Print the message variable
Write a print statement to display the message variable exactly as it is.
Python
Hint
Use print(message) to show the string with escape characters interpreted.
4
Print the raw string with escape characters visible
Print the message variable so that the output shows the escape characters like \n and \t as text, not as new lines or tabs. Use the repr() function inside the print statement.
Python
Hint
Use print(repr(message)) to show the string with escape characters visible.
Practice
(1/5)
1. What does the escape character \n do in a Python string?
easy
A. Ends the string
B. Inserts a tab space
C. Prints a backslash character
D. Starts a new line in the output
Solution
Step 1: Understand escape characters
Escape characters are special sequences starting with a backslash that change how text is displayed.
Step 2: Identify the effect of \n
The sequence \n means a new line, so it moves the cursor to the next line when printing.
Final Answer:
Starts a new line in the output -> Option D
Quick Check:
\n = new line [OK]
Hint: Remember \n means new line, like pressing Enter [OK]
Common Mistakes:
Confusing \n with tab space
Thinking \n prints literally as \n
Mixing up \n with backslash
2. Which of the following is the correct way to print a backslash character in Python?
easy
A. print("/\")
B. print("\\")
C. print("\")
D. print("\n")
Solution
Step 1: Recognize backslash escape
Backslash is an escape character, so to print it literally, you must escape it with another backslash.
Step 2: Check each option
print("\\") uses \\ which prints a single backslash. print("\") is invalid syntax. print("/\") mixes slash and backslash incorrectly. print("\n") prints a new line, not a backslash.
Final Answer:
print("\\") -> Option B
Quick Check:
Double backslash prints one backslash [OK]
Hint: Use double backslash \\ to print one backslash [OK]
Common Mistakes:
Using a single backslash causes syntax error
Confusing forward slash / with backslash \
Expecting \n to print a backslash
3. What will be the output of this code?
print("Hello\nWorld\t!")
medium
A. Hello\nWorld\t!
B. Hello
World !
C. Hello
World !
D. Hello World !
Solution
Step 1: Interpret escape sequences in the string
\n creates a new line, so "Hello" and "World" appear on separate lines. \t adds a tab space before the exclamation mark.
Step 2: Visualize the output
The output shows "Hello" on the first line, then "World" followed by a tab space and "!" on the second line.
Final Answer:
Hello
World ! -> Option C
Quick Check:
\n = new line, \t = tab space [OK]
Hint: \n breaks line, \t adds spaces like tab [OK]
Common Mistakes:
Printing escape sequences literally instead of interpreting
Ignoring tab space effect
Confusing \n with space
4. Find the error in this code snippet:
print("She said, "Hello!"")
medium
A. Missing escape characters for inner quotes
B. Incorrect use of single quotes
C. Extra parentheses in print
D. No error, code runs fine
Solution
Step 1: Analyze string quotes
The string uses double quotes outside and inside without escaping inner quotes, causing syntax error.
Step 2: Fix by escaping inner quotes
Inner quotes around "Hello!" must be escaped as \"Hello!\" or use single quotes outside.
Final Answer:
Missing escape characters for inner quotes -> Option A
Quick Check:
Escape inner quotes to avoid syntax error [OK]
Hint: Escape inner quotes with \" or use single quotes outside [OK]
Common Mistakes:
Not escaping inner quotes causes syntax error
Using mismatched quotes
Assuming print syntax error instead of string error
5. You want to print this exact text including quotes and backslash:
He said: "Use \n for new lines."
Which print statement will produce this output?
hard
A. print("He said: \"Use \\n for new lines.\"")
B. print('He said: "Use \\n for new lines."')
C. print("He said: 'Use \n for new lines.'")
D. print('He said: "Use \n for new lines."')
Solution
Step 1: Identify required output characters
The output must include double quotes around the phrase and a literal backslash followed by n (not a new line).
Step 2: Escape quotes and backslash correctly
Double quotes inside string must be escaped as \". Backslash must be escaped as \\ to print literally. So \\n prints \n, not newline.
Step 3: Check options
print("He said: \"Use \\n for new lines.\"") correctly escapes inner quotes and backslash. print('He said: "Use \\n for new lines."') uses single quotes outside but escapes inner quotes unnecessarily and may cause confusion. print("He said: 'Use \n for new lines.'") uses single quotes inside output, not double quotes. print('He said: "Use \n for new lines."') prints \n as newline, not literal.
Final Answer:
print("He said: \"Use \\n for new lines.\"") -> Option A
Quick Check:
Escape quotes and backslash to print exactly [OK]
Hint: Escape " as \" and \ as \\ to print literally [OK]