Skip to content
Merged
36 changes: 18 additions & 18 deletions Doc/tutorial/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,25 @@ and uses the ``j`` or ``J`` suffix to indicate the imaginary part

.. _tut-strings:

Texts
Text
-------

Different kinds of text have the type :class:`str`. This includes
characters "``!``", words "``rabbit``", names "``Paris``", sentences
"``Got your back.``", etc. "``Yay! :)``". They can be enclosed in single
quotes (``'...'``) or double quotes (``"..."``) with the same result [#]_.
``\`` can be used to escape quotes::
Python can manipulate text (represented by type :class:`str`, so called
"strings") as well as numbers. This includes characters "``!``", words
"``rabbit``", names "``Paris``", sentences "``Got your back.``", etc.
"``Yay! :)``". They can be enclosed in single quotes (``'...'``) or double
quotes (``"..."``) with the same result [#]_.

>>> 'spam eggs' # single quotes
'spam eggs'
>>> "Paris rabbit got your back :)! Yay!" # double quotes
'Paris rabbit got your back :)! Yay!'
>>> '1975' # digits and numerals enclosed in quotes are also strings
'1975'

To quote a quote, we need to "escape" it, by preceding it with ``\``.
Alternatively, we can use the other type of quotation marks::

>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
Expand All @@ -160,23 +168,15 @@ quotes (``'...'``) or double quotes (``"..."``) with the same result [#]_.
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'

In the interactive interpreter, the output string is enclosed in quotes and
special characters are escaped with backslashes. While this might sometimes
look different from the input (the enclosing quotes could change), the two
strings are equivalent. The string is enclosed in double quotes if
the string contains a single quote and no double quotes, otherwise it is
enclosed in single quotes. The :func:`print` function produces a more
In the interactive interpreter, the string definition and output string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"In the Python shell..."

can look different. The :func:`print` function produces a more
readable output, by omitting the enclosing quotes and by printing escaped
and special characters::

>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
>>> s # without print(), special characters are included in the string
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
>>> print(s) # with print(), special characters are interpreted, so \n produces new line
First line.
Second line.

Expand Down