89

Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.

demongolem
  • 9,474
  • 36
  • 90
  • 105
ensnare
  • 40,069
  • 64
  • 158
  • 224
  • 4
    Should be explained in the question, but: http://php.net/print_r – AJ. Feb 02 '10 at 21:28
  • 1
    Mark: it's a PHP function that displays on screen the contents of any variable, including arrays or objects. – Álvaro González Feb 02 '10 at 21:28
  • print_r is a function in PHP which can output an any dimension array in a format which shows the keys and values. – Waleed Amjad Feb 02 '10 at 21:29
  • see the highest rated answer (not the accepted answer) on http://stackoverflow.com/questions/192109/is-there-a-function-in-python-to-print-all-the-current-properties-and-values-of – cwd Aug 24 '12 at 18:44
  • 3
    no, it shouldn't be explained in question. it simply requires basic knowledge of both php and python. if you don't understand question - it's not question for you. – tishma Feb 24 '17 at 14:06
  • The title of the question should specify this is for arrays only, in PHP print_r does much more because its compatible with almost any input (all props of an object, etc) – Ivan Castellanos Aug 31 '18 at 21:35

9 Answers9

53

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.

Community
  • 1
  • 1
Robert Christie
  • 20,177
  • 8
  • 42
  • 37
19

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) )
GetFree
  • 40,278
  • 18
  • 77
  • 104
10
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)
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
7

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

sha256
  • 3,029
  • 1
  • 27
  • 32
5

print and pprint are great for built-in data types or classes which define a sane object representation. If you want a full dump of arbitrary objects, you'll have to roll your own. That is not that hard: simply create a recursive function with the base case being any non-container built-in data type, and the recursive case applying the function to each item of a container or each attribute of the object, which can be gotten using dir() or the inspect module.

Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
2

there is print_r for python https://github.com/marcbelmont/python-print_r/wiki but it is better to use standard modules

jellyfish
  • 596
  • 1
  • 7
  • 9
2
my_list = list(enumerate([1,2,3,4,5,6,7,8,9],0))

print(my_list)

Will print [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]

0

if you want to format variable as a string you can do:

s = repr(variable)

it works regardless the type, and doesn't requiere any imports.

If you want to include the contents of an object in a single string together with its type:

if hasattr(variable, "__dict__"):
    s = "{}: {}".format(variable, vars(variable))
else:
    s = repr(variable)
AlejandroVD
  • 1,576
  • 19
  • 22
0

For simple test views I use HttpResponse. Testing request from form, variable, string, id, not Object

from django.http.response import HttpResponse
HttpResponse(variable)

def add_comment(request, id):
return HttpResponse(id)
Ivan Pirus
  • 1,026
  • 11
  • 21