Question: I am looking for a way to quickly print a variable name and value while rapidly developing/debugging a small Python script on a Unix command line/ssh session.

It seems like a very common requirement and it seems wasteful (on keystrokes and time/energy) to duplicate the `variable_name`s on every line which prints or logs its value. i.e. rather than

```
print 'my_variable_name:', my_variable_name

```

I want to be able to do the following for [`str`](https://docs.python.org/3.7/library/stdtypes.html#str), [`int`](https://docs.python.org/3.7/library/functions.html#int), [`list`](https://docs.python.org/3.7/tutorial/introduction.html#lists), [`dict`](https://docs.python.org/3.7/tutorial/datastructures.html#dictionaries):

```
log(my_variable_name)
log(i)
log(my_string)
log(my_list)

```

and get the following output:

```
my_variable_name:some string
i:10
my_string:a string of words
my_list:[1, 2, 3]

```

Ideally the output would also log the function name.

I have seen some solutions attempting to use `locals`, `globals`, frames etc., But I have not yet seen something that works for ints, strings, lists, and works inside functions too.

Thanks!

Answer: 
Sorry to Necro this ancient thread, but this was surprisingly difficult to find a good answer for.

Using the '=' sign after the variable achieves this. For instance:

```
import pathlib as pl
import logging

logging.basicConfig(level=logging.DEBUG)

data_root = pl.Path("D:\Code_Data_Dev\Data\CSV_Workspace_Data")

logging.debug(f'{data_root=}')

```

This outputs

```
DEBUG:root:data_root=WindowsPath('D:/Code_Data_Dev/Data/CSV_Workspace_Data')

```
Here is another evil hack:

```
import inspect

def log(a):
    call_line = inspect.stack()[1][4][0].strip()
    assert call_line.strip().startswith("log(")
    call_line = call_line[len("log("):][:-1]
    print "Log: %s = %s" % (call_line, a)

b=3
log(b)

```

This obviously would need some range checks, better parsing, etc.
Also works only if the calls is made always in the same way and has probably more - unknown to me - assumptions...