KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

What do you understand by escape character? Explain any five escape sequences used in Python with the help of examples.

Getting Started

2 Likes

Answer

An escape character is a special character used in a string to insert characters that cannot be written directly inside the string. In Python, the backslash (\) is used as an escape character. The combination of the backslash and another character is called an escape sequence.

Five escape sequences in Python:

1. \n (New line) — It is used to move the cursor to a new line.

Example:

print("Hello\nWorld")

Output:

Hello
World

2. \t (Tab space) — It is used to provide horizontal tab space in a string.

Example:

print("Python\tProgramming")

Output:

Python    Programming

3. \' (Single quote) — It is used to insert a single quote inside a string.

Example:

print('It\'s my pen')

Output:

It's my pen

4. \" (Double quote) — It is used to insert a double quote inside a string.

Example:

print("He said, \"Hello\"")

Output:

He said, "Hello"

5. \\ (Backslash) — It is used to insert a backslash character in a string.

Example:

print("The symbol is \\")

Output:

The symbol is \

Answered By

3 Likes


Related Questions