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.
import os
# Display all environment variables
print("\n--- Environment Variables ---")
for key, value in os.environ.items():
print(f"{key} = {value}")
import os
# Get a specific environment variable
username = os.environ.get("USERNAME") or os.environ.get("USER")
print(f"System username: {username}")
--- Environment Variables ---
PATH = /usr/bin:/bin:/usr/sbin:/sbin
HOME = /Users/shahin
LANG = en_US.UTF-8
...
System username: shahin
- 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")