Python Basics: Variables, Strings, and Print
Learn Python fundamentals essential for network automation: variables, strings, f-strings, and print functions. Includes practical networking examples with IP addresses, hostnames, and device configurations.
If you're starting your network automation journey, mastering Python's fundamental building blocks is essential. Today we'll explore variables, strings, and the print function. These are the foundation skills you'll use in every automation script you write.
Understanding Variables in Python
Variables in Python are containers that store data. Think of them as labeled boxes where you can keep information you'll need later. In networking contexts, variables help you manage device information efficiently.
Here's how to create variables for common networking data:
hostname = "R1-NYC-CORE"
ip_address = "192.168.1.1"
interface = "GigabitEthernet0/1"
vlan_id = 100
is_up = TrueNotice that Python automatically determines the data type. hostname and ip_address are strings (text), vlan_id is an integer (number), and is_up is a boolean (True/False).
Working with Strings
Strings are sequences of characters enclosed in quotes. You can use single quotes, double quotes, or triple quotes for multi-line strings:
device_name = "SW01-Building-A"
description = 'Production switch'
config_template = """
interface GigabitEthernet0/1
description Connection to server
switchport mode access
"""Python strings have powerful built-in methods for networking tasks:
hostname = "R1-NYC-CORE"
print(hostname.upper()) # R1-NYC-CORE
print(hostname.lower()) # r1-nyc-core
print(hostname.replace("-", "_")) # R1_NYC_COREThe Power of F-Strings
F-strings (formatted string literals) are the modern way to combine variables with text. They're incredibly useful for creating configuration commands and output messages:
hostname = "SW01"
ip_address = "10.1.1.10"
subnet_mask = "255.255.255.0"
# Traditional string concatenation (avoid this)
old_way = "Device " + hostname + " has IP " + ip_address
# F-string (modern and clean)
message = f"Device {hostname} has IP {ip_address}/{subnet_mask}"
print(message) # Device SW01 has IP 10.1.1.10/255.255.255.0F-strings really shine when generating configuration commands:
interface_name = "GigabitEthernet0/2"
vlan = 200
description = "Server VLAN"
config_command = f"""
interface {interface_name}
description {description}
switchport access vlan {vlan}
switchport mode access
no shutdown
"""
print(config_command)Using the Print Function
The print() function displays output to the screen. While simple, it's your primary tool for debugging scripts and displaying results:
# Basic printing
print("Starting network discovery...")
# Printing variables
device_count = 5
print(f"Found {device_count} devices")
# Printing multiple items
hostname = "R1"
status = "UP"
print("Device:", hostname, "Status:", status)You can customize print behavior with parameters:
# Change the separator
print("IP", "Hostname", "Status", sep=" | ")
# Output: IP | Hostname | Status
# Change the end character
print("Processing device", end="... ")
print("Complete!")
# Output: Processing device... Complete!Practical Networking Example
Let's combine everything into a practical script that processes network device information:
# Network device variables
devices = ["R1-NYC", "R2-CHI", "SW1-LAB"]
base_ip = "192.168.1"
starting_octet = 10
print("Network Device Configuration Report")
print("=" * 40)
for i, device in enumerate(devices):
ip_address = f"{base_ip}.{starting_octet + i}"
config_line = f"hostname {device}"
mgmt_line = f"interface management 1"
ip_line = f" ip address {ip_address} 255.255.255.0"
print(f"\nDevice: {device}")
print(f"Management IP: {ip_address}")
print("Configuration:")
print(f" {config_line}")
print(f" {mgmt_line}")
print(f" {ip_line}")
print(f" no shutdown")What's Next
Now that you understand Python variables, strings, and printing, you're ready to tackle more complex data structures. In our next post, we'll explore Python lists and dictionaries; essential tools for managing collections of network devices, IP addresses, and configuration data.