1

Thanks in advance for your help.

When entering "example" at the command line, Python returns 'example'. I can not find anything on the web to explain this. All reference materials speaks to strings in the context of the print command, and I get all of the material about using double quotes, singles quotes, triple quotes, escape commands, etc.

I can not, however, find anything explaining why entering text surrounded by double quotes at the command line always returns the same text surrounded by single quotes. What gives? Thanks.

Hunter
  • 11
  • 1

3 Answers3

6

In Python both 'string' and "string" are used to represent string literals. It's not like Java where single and double quotes represent different data types to the compiler.

The interpreter evaluates each line you enter and displays this value to you. In both cases the interpreter is evaluating what you enter, getting a string, and displaying this value. The default way of displaying strings is in single quotes so both times the string is displayed enclosed in single quotes.

It does seem odd - in that it breaks Python's rule of There should be one - and preferably only one - obvious way to do it - but I think disallowing one of the options would have been worse.

You can also enter a string literal using triple quotes:

>>> """characters
... and
... newlines"""
'characters\nand\nnewlines'

You can use the command line to confirm that these are the same thing:

>>> type("characters")
<type 'str'>
>>> type('characters')
<type 'str'>
>>> "characters" == 'characters'
True

The interpreter uses the __repr__ method of an object to get the display to print to you. So on your own objects you can determine how they are displayed in the interpreter. We can't change the __repr__ method for built in types, but we can customise the interpreter output using sys.displayhook:

>>> import sys
>>> def customoutput(value):
...     if isinstance(value,str):
...        print '"%s"' % value
...     else:
...        sys.__displayhook__(value)
...
>>> sys.displayhook = customoutput
>>> 'string'
"string"
Community
  • 1
  • 1
David Webb
  • 190,537
  • 57
  • 313
  • 299
1

In python, single quotes and double quotes are semantically the same. It struck me as strange at first, since in C++ and other strongly-typed languages single quotes are a char and doubles are a string. Just get used to the fact that python doesn't care about types, and so there's no special syntax for marking a string vs. a character. Don't let it cloud your perception of a great language

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 2
    Python does care about types (unlike Perl or PHP or Ruby or ...), but it indeed doesn't have an extra char type. But that's not the question - he was just confused by the fact that the REPL evaluates the input and displays it, even when not printing. –  Jul 23 '10 at 04:38
0

Don't get confused. In python single quotes and double quotes are same. The creates an string object.

Tauquir
  • 6,613
  • 7
  • 37
  • 48