What is the equivalent of php's print_r() in python?

Solution 1:

The python print statement does a good job of formatting multidimesion arrays without requiring the print_r available in php.

As the definition for print states that each object is converted to a string, and as simple arrays print a '[' followed by a comma separated list of object values followed by a ']', this will work for any depth and shape of arrays.

For example

>>> x = [[1,2,3],[4,5,6]]
>>> print x
[[1, 2, 3], [4, 5, 6]]

If you need more advanced formatting than this, AJs answer suggesting pprint is probably the way to go.

Solution 2:

You were looking for the repr bult-in function.
http://docs.python.org/2/library/functions.html#func-repr

print repr(variable)

In Python 3, print is no longer a statement, so that would be:

print( repr(variable) )

Solution 3:

from pprint import pprint

student = {'Student1': { 'Age':10, 'Roll':1 }, 
           'Student2': { 'Age':12, 'Roll':2 }, 
           'Student3': { 'Age':11, 'Roll':3 }, 
           'Student4': { 'Age':13, 'Roll':4 }, 
           'Student5': { 'Age':10, 'Roll':5 }
           }

pprint(student)

Solution 4:

here's one you can try:

https://github.com/sha256/python-var-dump

you can install it simply using pip

pip install var_dump

disclaimer: I wrote it :)