To check if the key exists (returns True
or False
)
'HOME' in os.environ
You can also use get()
when printing the key; useful if you want to use a default.
print(os.environ.get('HOME', '/home/username/'))
where /home/username/
is the default