Python Outputting Variables Lab

Lab 1: The “Legacy” vs “Modern” Challenge Try printing a server name and IP address using three methods.

server = "Prod-DB-01"
ip = "192.168.1.50"

# Method 1: Comma (Lazy but works)
print("Server:", server, "has IP:", ip)

# Method 2: Concatenation (Harder)
print("Server: " + server + " has IP: " + ip)

# Method 3: f-string (The Pro Way - Use this!)
print(f"Server: {server} has IP: {ip}")

Lab 2: Formatting Numbers DevOps Scenario: Displaying costs or percentages.

cost = 49.9999
print(f"Server Cost: ${cost:.2f}") 
# Output: Server Cost: $50.00 (Rounded automatically!)

Lab 3: The CLI Menu Builder (Using sep)

Scenario: You are building a script that asks the user to choose an environment (Dev, QA, Prod). You want to print a clean menu without writing print() five times.

Concept: The sep (separator) parameter.

print("--- AWS Environment Selector ---")

# Instead of 4 separate print statements, use one with 'sep' set to a new line (\n)
print("1. Development", "2. QA Testing", "3. Pre-Production", "4. Production", sep="\n")

print("--------------------------------")

#output
--- AWS Environment Selector ---
1. Development
2. QA Testing
3. Pre-Production
4. Production
--------------------------------

Lab 4: The “Aligned” Inventory Table (Advanced f-strings)

Scenario: You scraped a list of servers and their status. You need to print them in a neat table where columns align perfectly, even if the server names have different lengths.

Concept: F-string Padding/Alignment.

  • < : Align Left
  • > : Align Right
  • ^ : Align Center
  • 20: Reserve 20 spaces for this text.
# Variables with different lengths
s1_name, s1_status, s1_cpu = "web-server-01", "Running", 15
s2_name, s2_status, s2_cpu = "db-backup-primary", "Stopped", 0
s3_name, s3_status, s3_cpu = "api-gateway", "High-Load", 92

# The Header
print(f"{'SERVER NAME':<20} | {'STATUS':^12} | {'CPU %':>6}")
print("-" * 45) # Prints a divider line easily

# The Rows (Notice the padding numbers match the header)
print(f"{s1_name:<20} | {s1_status:^12} | {s1_cpu:>6}%")
print(f"{s2_name:<20} | {s2_status:^12} | {s2_cpu:>6}%")
print(f"{s3_name:<20} | {s3_status:^12} | {s3_cpu:>6}%")

#Output
SERVER NAME          |    STATUS    |  CPU %
---------------------------------------------
web-server-01        |   Running    |     15%
db-backup-primary    |   Stopped    |      0%
api-gateway          |  High-Load   |     92%

Why this matters: When you want a report, readability is everything. This logic is used in almost every CLI tool (like kubectl get pods).

Lab 5: The “Lazy Debugger” (Python 3.8+ Feature)

Scenario: Your script is failing. You need to quickly check the value of a variable, but you are tired of typing print("x =", x).

Concept: The = specifier inside f-strings. It prints the expression and the value together.

api_key = "Ax78-9900-Z"
timeout = 400

# The Old/Slow Way
print("api_key =", api_key)

# The Pro Way (Self-documenting expression)
print(f"{api_key=}")
print(f"{timeout=}")

# You can even do math inside it!
a = 10
b = 5
print(f"{a + b = }")

#Output
api_key = Ax78-9900-Z
api_key='Ax78-9900-Z'
timeout=400
a + b = 15

Lab 7: Path Construction (Avoiding Slash Confusion)

Scenario: You are working on Windows but your server is Linux. Windows uses backslash \ and Linux uses forward slash /. Hardcoding paths causes errors.

Concept: While os.path is the official way, print helps us visualize paths safely using “Raw Strings” (r"").

folder = "var"
subfolder = "log"
filename = "syslog.txt"

# Normal strings interpret '\' as an escape character (like \n for newline)
# So specific Windows paths fail.

# Using Raw String (r) treats backslashes as text, not commands.
windows_path = r"C:\Users\Admin\Documents" 

# Building a path dynamically
print(f"Linux Path: /{folder}/{subfolder}/{filename}")
print(f"Windows Path: {windows_path}")

#Output
Linux Path: /var/log/syslog.txt
Windows Path: C:\Users\Admin\Documents
Scroll to Top