0
>>> pkt = sniff(count=2,filter="tcp")
>>> raw  = pkt[1].sprintf('%Padding.load%')
>>> raw
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"


>>> print raw
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'

Raw yield different output when use print

8
  • 2
    Barring escaped backslashes, I don't see a difference. Commented Nov 21, 2012 at 19:05
  • print raw uses repr and just using raw uses str function. Commented Nov 21, 2012 at 19:06
  • I mean for difference in the output not in the value itself. I thought both use in str Commented Nov 21, 2012 at 19:06
  • repr does not have to use str. It can if it wants to. All it is require to do is return a string representation. Commented Nov 21, 2012 at 19:07
  • See also: Difference between __str__ and __repr__ in Python Commented Nov 21, 2012 at 19:07

2 Answers 2

5

One is the repr() representation of the string, the other the printed string. The representation you can paste back into the interpreter to make the same string again.

The Python interactive prompt always uses repr() when echoing variables, print always uses the str() string representation.

They are otherwise the same. Try print repr(raw) for comparison:

>>> "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
>>> print "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'
>>> print repr("'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'")
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
Sign up to request clarification or add additional context in comments.

Comments

1

__str__ and __repr__ built in methods of a class can return whatever string values they want. Some classes will simply use a str() for their repr.

class AClass(object):

   def __str__(self):
      return "aclass"

   def __repr__(self):
      return str(self)

class AClass2(AClass):

   def __repr__(self):
      return "<something else>"

In [2]: aclass = AC
AClass   AClass2  

In [2]: aclass = AClass()

In [3]: print aclass
aclass

In [4]: aclass
Out[4]: aclass

In [5]: aclass2 = AClass2()

In [6]: print aclass2
aclass

In [7]: aclass2
Out[7]: <something else>

In [8]: repr(aclass2)
Out[8]: '<something else>'

In [9]: repr(aclass)
Out[9]: 'aclass'

repr is simply meant to show a "label" of the class, such as when you print a list that contains a bunch of this instance...how it should look.

str is how to convert the instance into a proper string value to be used in operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.