Skip to content

02. Printing & Variables

Printing in Python

The print() function is one of the first things you’ll use in Python.
It is used to display output to the console (e.g., results of a scan, alerts, or logs).

Example:

print("Hello, Cybersecurity World!")
print("Target system detected")

This will output:

Hello, Cybersecurity World!
Target system detected

Cybersecurity Context:
Printing is used to show scan results, status messages, or alerts while running scripts.


Variables in Python

Variables are like containers that store data in your program.
They make it easy to reuse values (like IP addresses, ports, or credentials) without retyping.

Example:

# 02. Printing & Variables
ip_address = "192.168.1.10"
port = 22
service = "SSH"

print("Scanning Target:", ip_address)
print("Port:", port, "Service:", service)

Output:

Scanning Target: 192.168.1.10
Port: 22 Service: SSH

Rules for Variables

  • Must start with a letter or _ (underscore).

  • Can contain letters, numbers, and _.

  • Cannot start with a number.

  • Case-sensitive (ip and IP are different).

Good:

target_ip = "10.0.0.5"
username_list = ["admin", "root"]

Bad:

1ip = "10.0.0.5"   # ❌ starts with a number
user-name = "admin" # ❌ hyphen not allowed

Cybersecurity Use Cases

  1. Storing Targets
target_ip = "192.168.1.1"
print("Pinging:", target_ip)
  1. Port Information
port = 80
print("Checking HTTP service on port", port)
  1. Credentials (for testing only – educational)
username = "admin"
password = "toor"
print("Attempting login with:", username, "/", password)
  1. Dynamic Output with f-strings
ip = "10.10.10.5"
port = 443
print(f"Connecting to {ip} on port {port}")

Output:

Connecting to 10.10.10.5 on port 443

Quick Practice Tasks

  • Create a variable attacker_ip and print it.

  • Store a list of 3 ports (22, 80, 443) and print them.

  • Use f-string to print: "Scanning <IP> on <Port>".


Note

Variables should not be hardcoded with real credentials in production scripts. For cybersecurity tools, store sensitive data securely (config files, environment variables).