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

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


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.


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) )


from pprint import pprintstudent = {'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)