Part of the series

Several example codes

~1 min read • Updated Oct 1, 2025

Program Overview

This Python program accesses system environment variables and displays their values.
Environment variables contain system-level information such as user names, file paths, network settings, and runtime configurations.
The program uses the built-in os module to retrieve and display these variables.


Python Code:


import os

# Display all environment variables
print("\n--- Environment Variables ---")
for key, value in os.environ.items():
    print(f"{key} = {value}")

Access a Specific Variable:


import os

# Get a specific environment variable
username = os.environ.get("USERNAME") or os.environ.get("USER")
print(f"System username: {username}")

Sample Output:


--- Environment Variables ---
PATH = /usr/bin:/bin:/usr/sbin:/sbin
HOME = /Users/shahin
LANG = en_US.UTF-8
...
System username: shahin

Step-by-Step Explanation:

- The os module is part of Python’s standard library
- os.environ returns a dictionary of all environment variables
- You can loop through this dictionary to display all key-value pairs
- To access a specific variable, use os.environ.get("NAME")


Written & researched by Dr. Shahin Siami