-1

I have a very basic query regarding Python (2.7.3) strings. What is the difference to the Python interpreter between

someString = 'foo bar'
someString

and

print someString

The first one produces 'foo bar' and the second produces foo bar.

tchakravarty
  • 10,736
  • 12
  • 72
  • 116
  • 1
    First one returns the `repr` version of string and second one returns the `str()` version. Related: [Difference between `__str__` and `__repr__` in Python](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) – Ashwini Chaudhary Sep 16 '13 at 10:00
  • 1
    No idea why this is downvoted to oblivion; I think this can actually confuse people when starting to learn Python from the interactive interpreter. – poke Sep 16 '13 at 10:08
  • @poke Thanks for the support. H8ers gon' h8? ;) – tchakravarty Sep 16 '13 at 10:11

1 Answers1

2

Just someString will do mostly nothing but return the someString value. In the interactive interpreter, return values cause the interpreter to print the repr value of it. repr(someString) will result in 'foo bar' a representation of the string that is itself valid Python code. If you don’t run it with an interactive interpreter, that line will simply return the string’s value but otherwise do nothing (i.e. the value is then thrown away).

The print statement will however execute the statement and print the value to the standard system output, usually your console window. So in a non-interactive interpreter session, this is something you will still see.

poke
  • 369,085
  • 72
  • 557
  • 602