@@ -138,16 +138,25 @@ and uses the ``j`` or ``J`` suffix to indicate the imaginary part
138
138
139
139
.. _tut-strings :
140
140
141
- Strings
142
- -------
141
+ Text
142
+ ----
143
143
144
- Besides numbers, Python can also manipulate strings, which can be expressed
145
- in several ways. They can be enclosed in single quotes (``'...' ``) or
146
- double quotes (``"..." ``) with the same result [# ]_. ``\ `` can be used
147
- to escape quotes::
144
+ Python can manipulate text (represented by type :class: `str `, so-called
145
+ "strings") as well as numbers. This includes characters "``! ``", words
146
+ "``rabbit ``", names "``Paris ``", sentences "``Got your back. ``", etc.
147
+ "``Yay! :) ``". They can be enclosed in single quotes (``'...' ``) or double
148
+ quotes (``"..." ``) with the same result [# ]_.
148
149
149
150
>>> ' spam eggs' # single quotes
150
151
'spam eggs'
152
+ >>> " Paris rabbit got your back :)! Yay!" # double quotes
153
+ 'Paris rabbit got your back :)! Yay!'
154
+ >>> ' 1975' # digits and numerals enclosed in quotes are also strings
155
+ '1975'
156
+
157
+ To quote a quote, we need to "escape" it, by preceding it with ``\ ``.
158
+ Alternatively, we can use the other type of quotation marks::
159
+
151
160
>>> 'doesn\'t' # use \' to escape the single quote...
152
161
"doesn't"
153
162
>>> "doesn't" # ...or use double quotes instead
@@ -159,23 +168,14 @@ to escape quotes::
159
168
>>> '"Isn\'t," they said.'
160
169
'"Isn\'t," they said.'
161
170
162
- In the interactive interpreter, the output string is enclosed in quotes and
163
- special characters are escaped with backslashes. While this might sometimes
164
- look different from the input (the enclosing quotes could change), the two
165
- strings are equivalent. The string is enclosed in double quotes if
166
- the string contains a single quote and no double quotes, otherwise it is
167
- enclosed in single quotes. The :func: `print ` function produces a more
168
- readable output, by omitting the enclosing quotes and by printing escaped
169
- and special characters::
171
+ In the Python shell, the string definition and output string can look
172
+ different. The :func: `print ` function produces a more readable output, by
173
+ omitting the enclosing quotes and by printing escaped and special characters::
170
174
171
- >>> '"Isn\'t," they said.'
172
- '"Isn\'t," they said.'
173
- >>> print('"Isn\'t," they said.')
174
- "Isn't," they said.
175
175
>>> s = 'First line.\nSecond line.' # \n means newline
176
- >>> s # without print(), \n is included in the output
176
+ >>> s # without print(), special characters are included in the string
177
177
'First line.\nSecond line.'
178
- >>> print(s) # with print(), \n produces a new line
178
+ >>> print(s) # with print(), special characters are interpreted, so \n produces new line
179
179
First line.
180
180
Second line.
181
181
0 commit comments