Python Escape Characters and Raw String

Python Escape Characters

In Python, escape characters allow you to include special characters in a string that would otherwise be difficult or impossible to represent. An escape character is a backslash \ followed by a specific character that has a special meaning.

Example:

print(“Hello\nWorld”)
# Output:
# Hello
# World

Escape Character Description Example Output
\\ Backslash: If you want to print ‘\’ then we use \ print(“This is a backslash: \\”) This is a backslash: \
\n Newline print(“Hello\nWorld”) Hello World
\t Tab print(“Name\tAge”) Name Age
\b Backspace print(“Helloo\b”) Hello
\0 Null Character print(“Hello\0World”) Hello World

Raw String: A raw string is a string where escape sequences (like \n, \t, etc.) are treated as literal characters. You create a raw string by prefixing the string with r or R.

Example:

#without ‘r’ (Raw string)
print(”Hello\tWorld”)
#output: Hello World

#with ‘r’ (Raw string)
print(r”Hello\tWorld”)
#output: Hello\tWorld

Course Video

Course Video English:

Task

1. Write a Python program that prints the following text using escape sequences
Hello, it’s a beautiful day!
“Programming is fun,” she said.

2. Write a Python program that prints the file path C:\Program Files\MyApp using raw string.

3. Write a Python program that prints the following table using escape sequences the existing format is like this – NameAgeAlice30Bob25
Expected Output :
Name Age
Alice 30
Bob 25

4. Write a Python program that uses the backspace \b escape sequence to correct the following string from Helllo World to Hello World.

5. Write a Python program that inserts a null character \0 between two words (Hello\0World) and prints the string. For example, the string should be Hello World.

6. Write a Python program that prints the string Hello\nWorld exactly as it is, without interpreting the \n as a newline character using raw string.

Task Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

Escape characters include sequences like \n (new line), \t (tab), \\ (backslash), and \’ (single quote) to represent special characters.

Use a backslash \ before a character to escape it, allowing Python to interpret it as a literal rather than a special character.

The escape sequence \t is used in Python to insert a horizontal tab space in strings.

Escape sequences are combinations of characters starting with a backslash (\) that represent special characters, e.g., \n for a new line.

Special characters are escape sequences like \n, \t, \\, \’, and \” used to represent non-printable or reserved characters in strings.

The double backslash \\ is used to represent a literal backslash in strings because a single backslash is used as an escape character.

A Python sequence is an ordered collection of elements, such as lists, tuples, strings, and ranges, which support indexing and iteration.

Use raw strings by prefixing the string with r, e.g., r”C:\Path”, to treat backslashes as literal characters instead of escape sequences.

A raw string in Python, prefixed with r, treats backslashes as literal characters, ignoring their role as escape characters.

Prefix the string with r, for example, r”example\path”, to ensure Python treats the string as raw, interpreting backslashes literally

 

 
4o