Learnitweb

Python Escape Characters, Comments, and Constants

In this tutorial, we cover three important topics:

• Escape characters in Python
• Writing comments (single-line and multi-line)
• Whether constants exist in Python

These topics are fundamental for writing clean, readable, and maintainable Python programs.
This tutorial follows the same detailed formatting as your earlier Python tutorials.


1. Escape Characters in Python

Escape characters are special sequences used inside strings to represent characters that cannot be typed directly or have special meaning.
They begin with a backslash (\).

Escape characters are common in languages like C, C++, Java, and Python.

Some commonly used escape characters:

Escape CharacterMeaning
\nNew line
\tHorizontal tab
\rCarriage return
\bBackspace
\fForm feed (next page)
\'Single quote symbol
\"Double quote symbol
\\Backslash symbol

Let us understand each one with examples.


2. Newline (\n)

\n inserts a new line.

Example:

print("Durga\nSoftware")

Output:

Durga
Software

3. Tab Space (\t)

\t inserts a horizontal tab (similar to pressing Tab key).

Example:

print("Durga\tSoftware")

Output will appear with spaces between Durga and Software.


4. Carriage Return (\r)

\r moves the cursor back to the beginning of the current line.

If a string is printed like:

print("Hello World\rHi")

Output:

Hillo World

Because the cursor returned to the start of the line before printing “Hi”.

(Not used very often, but exists.)


5. Backspace (\b)

\b deletes the previous character.

Example:

print("Hello\bPython")

Removes o from Hello before printing Python.


6. Form Feed (\f)

\f indicates page break.
Mostly used in printers or old formatting logic. Rarely used in modern Python.


7. Escape Characters for Quotes and Backslash

Sometimes you want to print special characters inside a string:

Printing a single quote inside single-quoted string

print('This is a single quote: \' ')

Printing a double quote inside double-quoted string

print("This is a double quote: \" ")

Printing a backslash itself

print("This is a backslash: \\")

Without escaping, Python interprets these characters as part of the string syntax and throws syntax errors.


8. Escape Characters in File Paths

Windows paths use backslashes:

D:\Durga\classes

If you write this directly in Python:

print("D:\Durga\classes")

Python may interpret sequences like \D, \c as escape sequences.

Correct approaches:

Using double backslashes

print("D:\\Durga\\classes")

Using raw strings

print(r"D:\Durga\classes")

The r"" prefix means “raw string” where escape sequences are ignored.


9. Comments in Python

Comments are notes written for human understanding.
Python completely ignores comments during execution.

9.1 Single-Line Comments

In Python, single-line comments start with the # symbol.

Example:

# This is a single-line comment
print("Hello")

Python ignores everything after the # on that line.

9.2 Multi-Line Comments

Python does not have a built-in multi-line comment syntax like Java or C.

Examples from Java:

// single line comment

/*
multi
line
comment
*/

Python does not support this style.

Correct way in Python

If you want to comment multiple lines:

# line 1
# line 2
# line 3

Each line must be preceded by a #.

Misconception: Triple Quotes

Some books incorrectly suggest using triple quotes as multi-line comments:

"""
Line 1
Line 2
"""

This is not a comment.
This is a docstring (documentation string).
Docstrings are stored at runtime and used for documentation generation, not comments.


10. Demonstration of Comments

Example:

# This line is commented and will not execute
print("This is a comment demo")

Output:

This is a comment demo

If both lines are commented:

# print("Hello")
# print("World")

No output will be printed.


11. Constants in Python

In other programming languages (like Java, C++, etc.), we can define constants:

Example in Java:

final int x = 10;
x = 20;   // Error: cannot assign to final variable

Python, however, does not support constants natively.

There is:

• No final keyword
• No constant declaration syntax

Variables can be reassigned freely.

Example:

MAX_VALUE = 10
MAX_VALUE = 20   # Allowed

Even though the name is uppercase, Python allows reassignment.

11.1 Then what is the purpose of uppercase?

It is just a convention, not a rule.

• If you write variable names in UPPERCASE,
• It indicates to other programmers that this value should not be changed

But Python does not enforce this.

Example:

PI = 3.14
PI = 3.14159   # Python allows this

Thus:

• Constants are NOT supported technically
• Only naming conventions exist

Reason:

Python has no variable declaration phase.
You directly assign values to variables, and Python allows reassignments freely.


12. Summary

• Escape characters begin with a backslash and represent special symbols like newline, tab, etc.
• Use \n, \t, \\, \", \' to print special characters correctly.
• Python supports only single-line comments using #.
• Python does not have true multi-line comments; triple quotes are docstrings, not comments.
• Constants do not exist in Python. Uppercase variable names are only a convention, not enforced.