Python Escape Characters Lab

Lab: Try it Yourself!

Open a Python terminal or any online compiler and run this code:

# Lab 1: The messy vs clean string
print("Name: DevSecOps Guru\nRole: Architect\tLocation: India")

# Lab 2: Handling quotes
print("He said, \"Learning DevSecOps is 100% worth it!\"")

# Lab 3: The backslash struggle
print("The folder is located at C:\\Program Files\\Scripts")

Lab 1: Python – The “Broken” String Try running this without the backslash and watch it fail. Then fix it.

# BROKEN CODE
# text = 'It's a beautiful day' 

# FIXED CODE
text = 'It\'s a beautiful day'
print(text)

Lab 1: The “Restaurant Menu” (Formatting with \n and \t)

Goal: Create a clean, aligned menu using only one print statement. Concept:

  • \n (New Line): Moves text to the next line.
  • \t (Tab): Adds spacing to align prices.

Code:

# Lab 1: Formatting a Menu
print("--- INDIAN STREET FOOD MENU ---")

# Without Escape Characters (Messy)
print("Item Price")
print("Samosa 20 INR")
print("Vada Pav 50 INR")

print("\n--- FIXED VERSION ---")

# With Escape Characters (Clean)
# Notice we use only ONE print() function for the whole list!
print("Item\t\tPrice\n----\t\t-----\nSamosa\t\t20 INR\nVada Pav\t50 INR\nChai\t\t10 INR")

Expected Output: You will see a perfectly aligned table. The \t pushes the price to the right column, and \n starts the next item on a new line.


Lab 2: The “Storyteller” (Handling Quotes \' and \")

Goal: Print a sentence that contains both single and double quotes without causing a syntax error. Concept:

  • \': Escapes a single quote inside a single-quoted string.
  • \": Escapes a double quote inside a double-quoted string.

Code:

# Lab 2: Handling Dialogue

# Scenario: We need to print: The developer said, "It's working!"

# Attempt 1: This will fail (Syntax Error)
# print('The developer said, "It's working!"') 

# Attempt 2: The Fix
print('The developer said, "It\'s working!"') 

# Alternative Fix (Swapping outer quotes)
print("The developer said, \"It's working!\"")

# Advanced: Both inside one string
print("My manager asked: \"Is the 'deploy' button ready?\"")

Lab 3: The “Windows Path” Struggle (Backslash \\)

Goal: Print a Windows file path correctly. This is a very common issue for Python beginners on Windows. Concept:

  • \\: Prints a literal backslash.

Code:

# Lab 3: File Paths

# Incorrect Way (Python thinks \U is an escape sequence start)
# path = "C:\Users\Admin\Documents"  <-- This causes an error!

# Correct Way 1: Double Backslash
path_fixed = "C:\\Users\\Admin\\Documents\\SecretFile.txt"
print("Path 1:", path_fixed)

# Correct Way 2: Raw Strings (The Developer's Favorite)
# putting 'r' before the quote tells Python to ignore ALL escape characters.
path_raw = r"C:\Users\Admin\Documents\SecretFile.txt"
print("Path 2:", path_raw)

Lab 4: The “DevSecOps Log” (Simulating Data Sanitization)

Goal: Visualize how raw data looks versus “escaped” data. This is crucial for understanding security logs. Concept:

  • repr(): A Python function that shows the “raw” representation of a string, including the escape characters.

Code:

# Lab 4: Security Logging Simulation

user_input = "Admin\nDelete_All"

print("--- VISUALIZING THE ATTACK ---")

# 1. Normal Print (What the user intends to happen)
# This looks like two separate commands on the console.
print("User Input Executed:\n" + user_input)

# 2. Raw Representation (What the log file sees)
# This reveals the hidden \n character.
print("\nLog File Entry:")
print(repr(user_input))

Lab 5: Fun with Unicode (Hex Escape \u)

Goal: Print special symbols and emojis using their specific ID. Concept:

  • \uXXXX: Prints a 16-bit Unicode character.
  • \UXXXXXXXX: Prints a 32-bit Unicode character (for Emojis).

Code:

# Lab 5: Icons and Symbols

# 16-bit hex value for Checkmark and Copyright
print("\u2705 System Check Complete") 
print("\u00A9 2026 DevSecOps Guru")

# 32-bit hex value for Python Snake Emoji (Note the capital U and 8 digits)
print("I love coding in Python \U0001F40D") 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top