Important Notice:

Escape Sequences

Escape Sequences

3 views 2 min read

Escape Sequences in Python :-

Escape Sequence ऐसे विशेष Character Combinations होते हैं जो Backslash (\) से शुरू होते हैं। इनका उपयोग String के अंदर विशेष कार्य (Special Operations) करने के लिए किया जाता है, जैसे नई पंक्ति (New Line), Tab Space, Quotes प्रिंट करना आदि।

English:
An Escape Sequence is a special character combination that begins with a Backslash (\). It is used to perform special operations inside a string, such as inserting a new line, tab space, quotes, and more.

Common Escape Sequences -

Example 1: New Line (\n) -

print("Hello\nWorld")

Output -

Hello
World
 
 
Example 2: Horizontal Tab (\t) -
print("Name\tAge")

print("Rahul\t18")

Output -

Name    Age
Rahul   18
 

Example 3: Backslash (\\) -

print("C:\\Python\\Programs")

Output -

C:\Python\Programs
 

Example 4: Single Quote (\') -

print('It\'s Python')

Output -

It's Python
 

Example 5: Double Quote (\") -

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

Output -

He said, "Hello!"
 

Example 6: Backspace (\b) -

print("ABC\bD")

Output -

ABD
 

Example 7: Carriage Return (\r)

print("Hello\rHi")

Output -

Hillo

Note: \r Cursor को लाइन की शुरुआत में ले जाता है और बाद के Characters पहले वाले Characters को Replace कर देते हैं। अलग-अलग Console में Output थोड़ा भिन्न हो सकता है।

    Example 8: Form Feed (\f)

print("Hello\fWorld")

Output -

HelloWorld

    Example 9: Vertical Tab (\v)

print("Hello\vWorld")

Output -

Hello
       World

Note: \v का प्रभाव विभिन्न Operating Systems और Terminals में अलग-अलग हो सकता है।

Example 10: Alert (\a) -

print("\a")

Output -

Bell Sound if supported by the system)

Important Points

  • सभी Escape Sequences Backslash (\) से शुरू होते हैं।

  • इनका उपयोग String के अंदर विशेष Characters या Formatting के लिए किया जाता है।

  • \n और \t सबसे अधिक उपयोग किए जाने वाले Escape Sequences हैं।

  • Quotes (' और ") तथा Backslash (\) को Print करने के लिए भी Escape Sequences का उपयोग किया जाता है.

Related Notes