Nearby lessons

28 of 108

Python - Escape Characters

Introduction

Escape characters are special characters used inside string literals.

They start with a backslash (\) and give a special meaning to the next character.

Escape characters help us print special symbols, create new lines, insert tabs, and perform other formatting operations.

What are Escape Characters?

In Python, escape characters are used inside strings to perform special actions.

Each escape character begins with a backslash (\).

For example, \n creates a new line and \t inserts a horizontal tab.

Example 1 - New Line (\n)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
durga
software

Explanation

The \n escape character moves the cursor to the next line.

Everything after \n is printed on a new line.

Example 2 - Horizontal Tab (\t)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
durga    software

Explanation

The \t escape character inserts a horizontal tab space.

Example 3 - Using Double Quote Without Escape Character

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
File "", line 1
s = "This is " symbol"
                 ^
SyntaxError: invalid syntax

Explanation

Python treats the second double quote as the end of the string.

Therefore, the remaining text becomes invalid and causes a SyntaxError.

Example 4 - Using Double Quote With Escape Character

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
This is " symbol

Explanation

The \" escape character prints a double quote inside the string.

This allows us to include double quotes without ending the string.

Important Escape Characters

Escape Character Meaning
\n New Line
\t Horizontal Tab
\r Carriage Return
\b Back Space
\f Form Feed
\v Vertical Tab
\' Single Quote
\" Double Quote
\\ Backslash Symbol

Escape Characters Summary

Escape Character Description
\n Moves the cursor to the next line.
\t Inserts a horizontal tab.
\r Moves the cursor to the beginning of the current line.
\b Removes one character before the cursor.
\f Inserts a form feed.
\v Inserts a vertical tab.
\' Prints a single quote (').
\" Prints a double quote (").
\\ Prints a backslash (\).

Constants in Python

Python does not have a separate concept of constants.

By convention, variable names are written in uppercase if their values should not be changed.

However, Python does not prevent the value from being modified.

Example - Constant Naming Convention

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20

Explanation

Writing variable names in uppercase is only a naming convention.

Python still allows the value to be changed.

Important Notes

  • Escape characters are used inside string literals.
  • Every escape character starts with a backslash (\).
  • \n creates a new line.
  • \t inserts a horizontal tab.
  • Use \" and \' to print quotes inside strings.
  • Python does not support true constants.
  • Uppercase variable names are used only as a convention for constants.

Key Points

  • Escape characters provide special meaning inside strings.
  • They improve string formatting and readability.
  • Escape characters help print special symbols without syntax errors.
  • Python follows a naming convention for constants instead of enforcing them.

Quick Summary

Topic Description
Escape Characters Special characters used inside strings.
\n New Line
\t Horizontal Tab
\" Print Double Quote
\\ Print Backslash
Constants Uppercase names are used by convention.

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - range() Function