Practice: Escape Characters

« Read the chapter

Q1. Two lines

BeginnerCoding

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

BeginnerCoding

Print the words Name, Age and City separated by tab characters.

Goal: Use \t.

Q3. Print a quote

BeginnerCoding

Print: She said "yes" using escaped double quotes inside double quotes.

Goal: Escape quotes.

Q4. Apostrophe

BeginnerCoding

Print It's fine using single quotes around the string.

Goal: Escape quotes.

Q5. A backslash

IntermediateCoding

Print a path C:\data\file using a normal string.

Goal: Escape backslashes.

Q6. Raw path

IntermediateCoding

Print the path C:\new\test using a raw string.

Goal: Use raw strings.

Q7. Accent letter

IntermediateCoding

Print the word cafe with an accented final letter e (U+00E9) using a Unicode escape.

Goal: Use Unicode escapes.

Q8. Length of escapes

IntermediateCoding

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

AdvancedCoding

Print the letter with code 90 using chr.

Goal: Use chr.

Q10. Tab output

BeginnerOutput

What does this print?

Code
print("A\tB")
print(len("A\tB"))

Goal: Predict tabs.

Q11. Doubled backslash

BeginnerOutput

What does this print?

Code
print("one\\two")

Goal: Predict escapes.

Q12. Raw versus normal

IntermediateOutput

What does this print?

Code
print(len(r"\n"), len("\n"))

Goal: Compare raw strings.

Q13. Mixed quotes

IntermediateOutput

What does this print?

Code
print('He said "it\'s ok"')

Goal: Read quotes.

Q14. repr of escapes

IntermediateOutput

What does this print?

Code
print(repr("a\tb\n"))

Goal: Use repr.

Q15. Unicode names

AdvancedOutput

What does this print?

Code
print("\u0048\u0069", "\x48\x69", ord("H"))

Goal: Decode escapes.

Q16. Path with a hidden tab

BeginnerDebugging

This program prints a broken path. Fix it so it prints C:\temp\tools.

Code
print("C:\temp\tools")

Goal: Fix paths.

Q17. Quote clash

BeginnerDebugging

This code gives a SyntaxError. Fix it so it prints Don't stop.

Code
print('Don't stop')

Goal: Fix quotes.

Q18. Raw string ending

AdvancedDebugging

This raw string gives a SyntaxError. Fix it so it prints the text folder\.

Code
print(r"folder\")

Goal: Handle raw string limits.

Q19. Why escapes

BeginnerConceptual

Explain in one or two sentences why Python needs escape sequences.

Goal: Explain escapes.

Q20. When raw

IntermediateConceptual

Explain when a raw string is the better choice.

Goal: Choose raw strings.