Escape Sequences
In the world of computers, "Escaping" means telling the system to stop what it's doing and look at a special command. In Python, we use the Backslash \ to escape the standard rules of a string. "Escape sequences are common across almost every programming language."
Basically what we are saying is anything that is immediately after the \ should be treated as a string. To use a ' or " we need to use \' and \", the same applies to a \, to tell Python we want the backslash to be seen as text we need to use \\.
Why do we need to escape?
Imagine you are typing a sentence and you want to start a new line, or you want to use a quote mark without Python thinking the string is finished. Python sees a string like a straight road—the backslash is the "exit ramp" that lets you do something different.
The Most Common "Exits"
In Python, the backslash \ is a special character. When you pair it with certain letters, it creates a "command" inside your string:
- \n (The Newline): This is like hitting the Enter key in the middle of your text.
- \t (The Tab): This pushes your text over with a big indent, just like the Tab key.
- \\ (The literal Backslash): Because the backslash is a "special" key, if you want to actually print one, you have to type it twice!
- \' and \": These allow you to include quotes inside a string without Python thinking the string has ended.
Escape Sequence
Python is literal. If you don't give it precise instructions, it crashes. Your goal is to figure out how to bypass Python's default string behavior.
The Objective
Input this exact line into the PyBox and get it to output without a SyntaxError:
it's not 'my' fault the "string" is not a int('string')
The Constraints
- Wrap the entire statement in single quotes (' ').
- No multiple print() calls.
- No modifying the punctuation.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named escape_sequence.py in your part12 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1:
The Objective
Produce this specific output using one print() statement.
Use \t to align the columns and \n for the rows.
Language Level
Python High
C++ Low
Exercise 2:
Path Integrity:
The Objective
Windows paths often break strings.
Print this path exactly:
C:\Users\node\temp