3

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?

sawa
  • 165,429
  • 45
  • 277
  • 381
michas
  • 25,361
  • 15
  • 76
  • 121

1 Answers1

4

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
falsetru
  • 357,413
  • 63
  • 732
  • 636