Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.
-
4Should be explained in the question, but: http://php.net/print_r – AJ. Feb 02 '10 at 21:28
-
1Mark: 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
-
3no, 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 Answers
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.

- 1
- 1

- 20,177
- 8
- 42
- 37
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) )

- 40,278
- 18
- 77
- 104
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)

- 25,063
- 15
- 119
- 120
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 :)

- 3,029
- 1
- 27
- 32
-
python already has this with `import pprint` https://stackoverflow.com/a/27543327/42223 – dreftymac Sep 23 '22 at 15:16
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.

- 37,799
- 10
- 82
- 91
there is print_r for python https://github.com/marcbelmont/python-print_r/wiki but it is better to use standard modules

- 596
- 1
- 7
- 9
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)]
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)

- 1,576
- 19
- 22
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)

- 1,026
- 11
- 21