Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of ruby's string inspect() in python

I apologize up front for the dumbness of this question, but I can't figure it out and its driving me crazy.

In ruby I can do:

irb(main):001:0> s = "\t\t\n"  
=> "\t\t\n"  
irb(main):003:0> puts s  

=> nil  
irb(main):004:0> puts s.inspect  
"\t\t\n"  

Is there an equivalent of ruby's inspect function in python?

like image 523
anoncoward Avatar asked Jan 04 '11 00:01

anoncoward


2 Answers

repr():

>>> print repr('\t\t\n')
'\t\t\n'
like image 142
moinudin Avatar answered Oct 24 '22 03:10

moinudin


You can use repr or (backticks), I am doing the exactly the same things as you did above.

>>> s = "\t\t\n"
>>> s
'\t\t\n'
>>> print s


>>> repr(s)
"'\\t\\t\\n'"
>>> print repr(s)
'\t\t\n'
>>> print `s`
'\t\t\n'
like image 40
Senthil Kumaran Avatar answered Oct 24 '22 03:10

Senthil Kumaran