In ruby you can inspect any object with the inspect method:
For example:
print [1,"string",:symbol,[?l, ?i, ?s, ?t]].inspect
will print
[1, "string", :symbol, ["l", "i", "s", "t"]]
Is there any similar facility in python which allows me to print the content of some arbitrary variable?
Use repr. It returns a string containing a printable representation of an object. (similar to Object#inspect in Ruby)
>>> repr([1,"string", ':symbol', ['l', 'i', 's', 't']])
"[1, 'string', ':symbol', ['l', 'i', 's', 't']]"
BTW, there's no symbol literal (:symbol) or single character string literal (?x) in Python; replaced them with string literals in the above example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With