Chapter 6 Assignments: Escape Sequences in Python
Basic Level (10 Questions)
- Write a Python program to print a string that includes a newline escape sequence
\n
.
- Demonstrate how to use the tab escape sequence
\t
to format output in columns.
- Show an example of printing a backslash
\\
in a string.
- Write a program that uses the single quote escape sequence
\'
inside a string enclosed by single quotes.
- Write a program that uses the double quote escape sequence
\"
inside a string enclosed by double quotes.
- Print a string with a carriage return
\r
and explain its effect.
- Write a program demonstrating the backspace escape sequence
\b
by deleting characters from a printed string.
- Show how to print a string that contains both single and double quotes using escape sequences.
- Explain the effect of the alarm escape sequence
\a
and write a program that uses it.
- Write a Python string that spans multiple lines using newline escape sequences.
Intermediate Level (10 Questions)
- Create a program that prints a formatted table using tabs (
\t
) and newlines (\n
).
- Write a program to replace all backslashes in a Windows file path string with double backslashes.
- Explain and demonstrate how carriage return
\r
can be used to overwrite output on the same line.
- Write a program that prints a string with a mix of quotes and escaped characters correctly.
- Demonstrate the difference between printing a string with and without escape sequences using raw strings (prefix
r
).
- Write a program that prints a progress bar animation using carriage return
\r
.
- Explain why the backspace escape sequence
\b
may not work as expected in some environments.
- Create a string that includes an audible beep
\a
and test if the beep plays on your system.
- Write a program that prints a dialogue between two people using newline and tab escape sequences for formatting.
- Explain how escape sequences affect the length of a string. Write code to demonstrate your explanation.
Advanced Level (10 Questions)
- Write a Python program that reads a string input from the user and replaces all newline escape sequences with a space.
- Create a function that takes a multiline string and prints each line prefixed with a line number using escape sequences.
- Write a program that simulates a simple console clock updating every second using carriage return
\r
.
- Write code that demonstrates how to escape backslashes correctly in regular expressions within strings.
- Explain and demonstrate how combining escape sequences can be used to format complex output (e.g., tables with indents and line breaks).
- Create a program that logs errors by printing error messages with a beep sound
\a
and formatting using tabs and newlines.
- Write a program to clean a string of all escape characters and print the raw text only.
- Implement a text animation in the console using backspace
\b
to delete characters and rewrite them.
- Write a program that counts the number of escape sequences in a given string and outputs their types.
- Create a program that takes a file path input and converts it to a raw string format, escaping necessary characters.