Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In Python both <a href="http://docs.python.org/reference/lexical_analysis.html#strings" rel="nofollow noreferrer"><code>'string'</code> and <code>"string"</code> are used to represent string literals</a>. It's not like Java where single and double quotes represent different data types to the compiler. </p> <p>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. </p> <p>It does seem odd - in that it breaks Python's rule of <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow noreferrer"><em>There should be one - and preferably only one - obvious way to do it</em></a> - but I think disallowing one of the options would have been worse.</p> <p>You can also enter a string literal using triple quotes:</p> <pre><code>&gt;&gt;&gt; """characters ... and ... newlines""" 'characters\nand\nnewlines' </code></pre> <p>You can use the command line to confirm that these are the same thing:</p> <pre><code>&gt;&gt;&gt; type("characters") &lt;type 'str'&gt; &gt;&gt;&gt; type('characters') &lt;type 'str'&gt; &gt;&gt;&gt; "characters" == 'characters' True </code></pre> <p>The interpreter uses the <a href="https://stackoverflow.com/questions/1984162/purpose-of-pythons-repr"><code>__repr__</code> method</a> 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 <code>__repr__</code> method for built in types, but we can customise the interpreter output <a href="http://docs.python.org/library/sys.html#sys.displayhook" rel="nofollow noreferrer">using <code>sys.displayhook</code></a>:</p> <pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; def customoutput(value): ... if isinstance(value,str): ... print '"%s"' % value ... else: ... sys.__displayhook__(value) ... &gt;&gt;&gt; sys.displayhook = customoutput &gt;&gt;&gt; 'string' "string" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload