Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

php

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

like image 241
ensnare Avatar asked Feb 02 '10 21:02

ensnare


People also ask

What is Print_r used for?

The print_r() function is a built-in function in PHP and is used to print or display information stored in a variable.

Where does Print_r output?

The api. php file is in C:\Program Files\xampp\htdocs\project\app\api\api.


1 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.

like image 184
Robert Christie Avatar answered Oct 06 '22 08:10

Robert Christie