One of the easiest ways to print the name of a variable in Python is by making use of the varname module.
Make sure to make use of the pip command to get it installed first.
pip install varname
Name: varname
Version: 0.11.2
Summary: Dark magics about variable names in python.
Home-page: https://github.com/pwwang/python-varname
Author: pwwang
Author-email: pwwang@pwwang.com
License: MIT
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: executing
Required-by:
Documentation:
Core features:
- Retrieving names of variables a function/class call is assigned to from inside it, using varname.
- Retrieving variable names directly, using nameof
- Detecting next immediate attribute name, using will
- Fetching argument names/sources passed to a function using argname
Reference: https://github.com/pwwang/python-varname

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!