Chapter 6 Assignments: Escape Sequences in Python

Basic Level (10 Questions)

  1. Write a Python program to print a string that includes a newline escape sequence \n.
  2. Demonstrate how to use the tab escape sequence \t to format output in columns.
  3. Show an example of printing a backslash \\ in a string.
  4. Write a program that uses the single quote escape sequence \' inside a string enclosed by single quotes.
  5. Write a program that uses the double quote escape sequence \" inside a string enclosed by double quotes.
  6. Print a string with a carriage return \r and explain its effect.
  7. Write a program demonstrating the backspace escape sequence \b by deleting characters from a printed string.
  8. Show how to print a string that contains both single and double quotes using escape sequences.
  9. Explain the effect of the alarm escape sequence \a and write a program that uses it.
  10. Write a Python string that spans multiple lines using newline escape sequences.

Intermediate Level (10 Questions)

  1. Create a program that prints a formatted table using tabs (\t) and newlines (\n).
  2. Write a program to replace all backslashes in a Windows file path string with double backslashes.
  3. Explain and demonstrate how carriage return \r can be used to overwrite output on the same line.
  4. Write a program that prints a string with a mix of quotes and escaped characters correctly.
  5. Demonstrate the difference between printing a string with and without escape sequences using raw strings (prefix r).
  6. Write a program that prints a progress bar animation using carriage return \r.
  7. Explain why the backspace escape sequence \b may not work as expected in some environments.
  8. Create a string that includes an audible beep \a and test if the beep plays on your system.
  9. Write a program that prints a dialogue between two people using newline and tab escape sequences for formatting.
  10. Explain how escape sequences affect the length of a string. Write code to demonstrate your explanation.

Advanced Level (10 Questions)

  1. Write a Python program that reads a string input from the user and replaces all newline escape sequences with a space.
  2. Create a function that takes a multiline string and prints each line prefixed with a line number using escape sequences.
  3. Write a program that simulates a simple console clock updating every second using carriage return \r.
  4. Write code that demonstrates how to escape backslashes correctly in regular expressions within strings.
  5. Explain and demonstrate how combining escape sequences can be used to format complex output (e.g., tables with indents and line breaks).
  6. Create a program that logs errors by printing error messages with a beep sound \a and formatting using tabs and newlines.
  7. Write a program to clean a string of all escape characters and print the raw text only.
  8. Implement a text animation in the console using backspace \b to delete characters and rewrite them.
  9. Write a program that counts the number of escape sequences in a given string and outputs their types.
  10. Create a program that takes a file path input and converts it to a raw string format, escaping necessary characters.