Skip to content

Commit a9e8616

Browse files
committed
adding strings revised and output cleared
1 parent 7ce9e68 commit a9e8616

File tree

1 file changed

+71
-101
lines changed

1 file changed

+71
-101
lines changed

chapter01_introduction/sec04_strings.ipynb

Lines changed: 71 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"cell_type": "code",
17-
"execution_count": 30,
17+
"execution_count": null,
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
@@ -31,18 +31,9 @@
3131
},
3232
{
3333
"cell_type": "code",
34-
"execution_count": 33,
34+
"execution_count": null,
3535
"metadata": {},
36-
"outputs": [
37-
{
38-
"name": "stdout",
39-
"output_type": "stream",
40-
"text": [
41-
"36\n",
42-
"31\n"
43-
]
44-
}
45-
],
36+
"outputs": [],
4637
"source": [
4738
"print(len(string1))\n",
4839
"print(len(string2))\n",
@@ -56,24 +47,16 @@
5647
"source": [
5748
"## Concatenation \n",
5849
"\n",
59-
"Concatenation is a name for the operation where we one string and append it to the end of another string, yielding a new, longer string."
50+
"Concatenation is a name for the operation \n",
51+
"where we append one string to the end of another, \n",
52+
"yielding a new, longer string."
6053
]
6154
},
6255
{
6356
"cell_type": "code",
64-
"execution_count": 12,
57+
"execution_count": null,
6558
"metadata": {},
66-
"outputs": [
67-
{
68-
"name": "stdout",
69-
"output_type": "stream",
70-
"text": [
71-
"string1: Helm, warp one -- \n",
72-
"string2: Engage!\n",
73-
"string3: Helm, warp one -- Engage!\n"
74-
]
75-
}
76-
],
59+
"outputs": [],
7760
"source": [
7861
"string1 = \"Helm, warp one -- \"\n",
7962
"string2 = \"Engage!\"\n",
@@ -88,25 +71,16 @@
8871
"cell_type": "markdown",
8972
"metadata": {},
9073
"source": [
91-
"Whenever we concatenate two strings, the resulting string will have a length equal to the some of the substrings from which it was formed. "
74+
"Whenever we concatenate two strings, \n",
75+
"the resulting string will have a length \n",
76+
"equal to the sum of the substrings from which it was formed. "
9277
]
9378
},
9479
{
9580
"cell_type": "code",
96-
"execution_count": 13,
81+
"execution_count": null,
9782
"metadata": {},
98-
"outputs": [
99-
{
100-
"name": "stdout",
101-
"output_type": "stream",
102-
"text": [
103-
"18\n",
104-
"7\n",
105-
"25\n",
106-
"25\n"
107-
]
108-
}
109-
],
83+
"outputs": [],
11084
"source": [
11185
"print(len(string1))\n",
11286
"print(len(string2))\n",
@@ -120,56 +94,53 @@
12094
"source": [
12195
"## String Methods\n",
12296
"\n",
123-
"We'll get more into how objects work in subsequent chapters but it's time you learned that objects are associated with some functionality by means of *methods*. Methods are basically functions that act upon a given object. We can invoke an object's methods with syntax that looks like this `object.method_name()`. Just as we can provide inputs (also called arguments) to a function, e.g. `print(string1, string2)` some methods require additional inputs. \n",
97+
"We'll get more into how objects work in subsequent chapters \n",
98+
"but it's time you learned that objects are associated \n",
99+
"with some functionality by means of *methods*. \n",
100+
"Methods are basically functions that act upon a given object.\n",
101+
"We can invoke an object's methods with syntax \n",
102+
"that looks like this: `object.method_name()`. \n",
103+
"Just as we can provide inputs (also called arguments) \n",
104+
"to an ordinary function, e.g. `print(string1, string2)`,\n",
105+
"some methods require that we supply arguments. \n",
124106
"\n",
125-
"All strings come bundled with a few helpful methods. The following handy methods don't require any arguments: We can flip all alphabetic characters to either upper or lower case by calling the `.upper()` or `.lower()` methods, respectively (leaving numeric and special characters alone)."
107+
"Strings come bundled with a few helpful methods.\n",
108+
"The following handy methods don't require any arguments:\n",
109+
"We can flip all alphabetic characters \n",
110+
"to either *upper* or *lower* or *title* (capitalized initials)\n",
111+
"by calling the `.upper()`, `.lower()`, or `.title()` methods, \n",
112+
"respectively (all three leave numeric and special characters unaltered)."
126113
]
127114
},
128115
{
129116
"cell_type": "code",
130-
"execution_count": 27,
117+
"execution_count": null,
131118
"metadata": {},
132-
"outputs": [
133-
{
134-
"name": "stdout",
135-
"output_type": "stream",
136-
"text": [
137-
"regular: \t Helm, warp one -- Engage!\n",
138-
"upper: \t \t HELM, WARP ONE -- ENGAGE!\n",
139-
"lower: \t \t helm, warp one -- engage!\n",
140-
"original: \t Helm, warp one -- Engage!\n"
141-
]
142-
}
143-
],
119+
"outputs": [],
144120
"source": [
145121
"print(\"regular: \\t\", string3)\n",
146122
"print(\"upper: \\t \\t\", string3.upper())\n",
147123
"print(\"lower: \\t \\t\", string3.lower())\n",
124+
"print(\"title: \\t \\t\", string3.title())\n",
148125
"print(\"original: \\t\", string3)"
149126
]
150127
},
151128
{
152129
"cell_type": "markdown",
153130
"metadata": {},
154131
"source": [
155-
"Note that calling the method did not change the original string. In other words, `.upper()` is a function, that returns a new string (that is all upper cased), while leaving the underlying object unchanged. We can see this clearly here:"
132+
"Note that calling the method did not change the original string.\n",
133+
"In other words, `.upper()` is a function\n",
134+
"that returns a new string (that is all upper cased),\n",
135+
"while leaving the underlying object unchanged. \n",
136+
"We can see this clearly here:"
156137
]
157138
},
158139
{
159140
"cell_type": "code",
160-
"execution_count": 29,
141+
"execution_count": null,
161142
"metadata": {},
162-
"outputs": [
163-
{
164-
"name": "stdout",
165-
"output_type": "stream",
166-
"text": [
167-
"Helm, warp one -- Engage!\n",
168-
"HELM, WARP ONE -- ENGAGE!\n",
169-
"<class 'str'>\n"
170-
]
171-
}
172-
],
143+
"outputs": [],
173144
"source": [
174145
"upper_string = string3.upper()\n",
175146
"print(string3)\n",
@@ -181,7 +152,18 @@
181152
"cell_type": "markdown",
182153
"metadata": {},
183154
"source": [
184-
"Note that `upper_string` has no idea that it was produced by setting `string3` to upper case. All trace of the original capitalziation scheme is lost. As far as it's concerned, `upper_string` is just an ordinary string that happens to contain capital letters."
155+
"In fact, note that strings are immutable. \n",
156+
"Thus while we can convert a string into various\n",
157+
"other related strings, we can never alter the underlying string. \n",
158+
"We can even reassign a variable that once pointed at a given string\n",
159+
"to subsequently point at a new string, but this isn't the same thing\n",
160+
"as mutating a string.\n",
161+
"\n",
162+
"Note that `upper_string` has no idea \n",
163+
"that it was produced by setting `string3` to upper case. \n",
164+
"All trace of the original capitalziation scheme is lost.\n",
165+
"As far as it's concerned, `upper_string` is \n",
166+
"just an ordinary string that happens to contain capital letters."
185167
]
186168
},
187169
{
@@ -190,24 +172,20 @@
190172
"source": [
191173
"## Escape Sequences\n",
192174
"\n",
193-
"For certain special characters we can't just express them in a string literal. We need to use an escape sequence. One example is the newline character. To express a newline in a string literal we can't actually put a newline in the middle of the code. Instead, we use \"\\n\"."
175+
"For certain special characters we can't \n",
176+
"just express them in a string literal.\n",
177+
"We need to use an escape sequence. \n",
178+
"One example is the newline character.\n",
179+
"To express a newline in a string literal,\n",
180+
"we can't actually put a newline in the middle of the code.\n",
181+
"Instead, we use \"\\n\"."
194182
]
195183
},
196184
{
197185
"cell_type": "code",
198-
"execution_count": 3,
186+
"execution_count": null,
199187
"metadata": {},
200-
"outputs": [
201-
{
202-
"name": "stdout",
203-
"output_type": "stream",
204-
"text": [
205-
"a\n",
206-
"b\n",
207-
"c\n"
208-
]
209-
}
210-
],
188+
"outputs": [],
211189
"source": [
212190
"print(\"a\\nb\\nc\")"
213191
]
@@ -216,35 +194,27 @@
216194
"cell_type": "markdown",
217195
"metadata": {},
218196
"source": [
219-
"Similarly `\"\\t\"` indicates a horizontal tab, `\"\\\\\"` indicates a backslash, and `\\\"` represents a double quote. Note that we only need to escape double quotes inside a double-quote-enclosed string literal. Likewise, we only need to escape single quotes inside a single-quote-enclosed string literal. However, either kind of quotation mark *can* be indicated via an escape sequence, regardless of whether the string is double- or single-quote-enclosed. "
197+
"Similarly `\"\\t\"` indicates a horizontal tab,\n",
198+
"`\"\\\\\"` indicates a backslash, and `\\\"` represents a double quote.\n",
199+
"Note that we only need to escape double quotes \n",
200+
"inside a double-quote-enclosed string literal.\n",
201+
"Likewise, we only need to escape single quotes\n",
202+
"inside a single-quote-enclosed string literal.\n",
203+
"However, either kind of quotation mark\n",
204+
"*can* be indicated via an escape sequence,\n",
205+
"regardless of whether the string is double- or single-quote-enclosed. "
220206
]
221207
},
222208
{
223209
"cell_type": "code",
224-
"execution_count": 8,
210+
"execution_count": null,
225211
"metadata": {},
226-
"outputs": [
227-
{
228-
"ename": "SyntaxError",
229-
"evalue": "EOF while scanning triple-quoted string literal (<ipython-input-8-603cbfe7ffea>, line 4)",
230-
"output_type": "error",
231-
"traceback": [
232-
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-8-603cbfe7ffea>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m print(\"\"\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOF while scanning triple-quoted string literal\n"
233-
]
234-
}
235-
],
212+
"outputs": [],
236213
"source": [
237214
"print('\\\"')\n",
238215
"print('\"')\n",
239216
"print(\"\\\"\")"
240217
]
241-
},
242-
{
243-
"cell_type": "code",
244-
"execution_count": null,
245-
"metadata": {},
246-
"outputs": [],
247-
"source": []
248218
}
249219
],
250220
"metadata": {
@@ -263,7 +233,7 @@
263233
"name": "python",
264234
"nbconvert_exporter": "python",
265235
"pygments_lexer": "ipython3",
266-
"version": "3.6.5"
236+
"version": "3.7.4"
267237
}
268238
},
269239
"nbformat": 4,

0 commit comments

Comments
 (0)