Environment Variable Loader
Create a .env file containing a sample API_KEY, then load it using python-dotenv and print its value — without ever hardcoding the key in the script.
Approach: write the given key to a local .env file, call load_dotenv() to load it into the process environment, then read it back with os.environ.get() and print it.
Input: One line: the API key value to store.
Output: One line: the API key value, read back from the environment (not hardcoded).
abcd1234efgh5678
abcd1234efgh5678
- the input line contains no newline or '=' character
Hint 1
Write a line like f"API_KEY={api_key_value}\n" into a file named ".env".
Hint 2
load_dotenv() (with no arguments) reads .env from the current directory into os.environ.
Hint 3
os.environ.get("API_KEY") safely returns the loaded value (or None if missing), instead of hardcoding it anywhere in the script.
Writing the given value into a real .env file, then calling load_dotenv(), reproduces the exact workflow the notes describe: secrets live in a file separate from the source code, get loaded into the process environment at runtime, and are accessed via os.environ.get() rather than being hardcoded anywhere in the .py file itself.
Related Problems
- Password Hashing and VerificationEasy · pythonSolve Problem
- Signup Input ValidationEasy · pythonSolve Problem
- JWT Creation and Expiry CheckMedium · pythonSolve Problem