Practice: Escape Characters
Q1. Two lines
Print the words Hello and World on two separate lines using a single print call and one escape sequence.
Goal: Use \n.
Q2. Tab table row
Print the words Name, Age and City separated by tab characters.
Goal: Use \t.
Q3. Print a quote
Print: She said "yes" using escaped double quotes inside double quotes.
Goal: Escape quotes.
Q4. Apostrophe
Print It's fine using single quotes around the string.
Goal: Escape quotes.
Q5. A backslash
Print a path C:\data\file using a normal string.
Goal: Escape backslashes.
Q6. Raw path
Print the path C:\new\test using a raw string.
Goal: Use raw strings.
Q7. Accent letter
Print the word cafe with an accented final letter e (U+00E9) using a Unicode escape.
Goal: Use Unicode escapes.
Q8. Length of escapes
Print the length of the string made from a, a new line and b, written with an escape.
Goal: Count escape characters.
Q9. Character code
Print the letter with code 90 using chr.
Goal: Use chr.
Q10. Tab output
What does this print?
print("A\tB")
print(len("A\tB"))Goal: Predict tabs.
Q11. Doubled backslash
What does this print?
print("one\\two")Goal: Predict escapes.
Q12. Raw versus normal
What does this print?
print(len(r"\n"), len("\n"))Goal: Compare raw strings.
Q13. Mixed quotes
What does this print?
print('He said "it\'s ok"')Goal: Read quotes.
Q14. repr of escapes
What does this print?
print(repr("a\tb\n"))Goal: Use repr.
Q15. Unicode names
What does this print?
print("\u0048\u0069", "\x48\x69", ord("H"))Goal: Decode escapes.
Q16. Path with a hidden tab
This program prints a broken path. Fix it so it prints C:\temp\tools.
print("C:\temp\tools")Goal: Fix paths.
Q17. Quote clash
This code gives a SyntaxError. Fix it so it prints Don't stop.
print('Don't stop')Goal: Fix quotes.
Q18. Raw string ending
This raw string gives a SyntaxError. Fix it so it prints the text folder\.
print(r"folder\")Goal: Handle raw string limits.
Q19. Why escapes
Explain in one or two sentences why Python needs escape sequences.
Goal: Explain escapes.
Q20. When raw
Explain when a raw string is the better choice.
Goal: Choose raw strings.