|
11 | 11 | "- Exercises: 20 min\n",
|
12 | 12 | "\n",
|
13 | 13 | "**Questions**:\n",
|
14 |
| - "- \"How do I manipulate strings (text)?\"\n", |
| 14 | + "- How do I manipulate strings (text)?\n", |
15 | 15 | "\n",
|
16 | 16 | "**Learning Objectives**:\n",
|
17 | 17 | "- Become familiar with the [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string) [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) and its methods\n",
|
|
22 | 22 | "cell_type": "markdown",
|
23 | 23 | "metadata": {},
|
24 | 24 | "source": [
|
25 |
| - "## We can do things with strings\n", |
| 25 | + "## Operations can be applied to strings\n", |
26 | 26 | "\n",
|
27 |
| - "* We've already seen some operations that can be done with strings." |
| 27 | + "* We've already seen some operations, such as addition, that can be applied to strings." |
28 | 28 | ]
|
29 | 29 | },
|
30 | 30 | {
|
|
43 | 43 | "cell_type": "markdown",
|
44 | 44 | "metadata": {},
|
45 | 45 | "source": [
|
46 |
| - "* Remember that computers don't understand context." |
| 46 | + "* The addition operation doesn't come with a space. We'll need to insert it on our own." |
47 | 47 | ]
|
48 | 48 | },
|
49 | 49 | {
|
|
63 | 63 | "## Strings are made up of sub-strings\n",
|
64 | 64 | "\n",
|
65 | 65 | "* You can think of strings as a [sequence](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#sequence) of smaller strings or characters. \n",
|
66 |
| - "* We can access a piece of that sequence using `[]`." |
| 66 | + "* We can access a piece of that sequence using bracket notation, `[]`." |
67 | 67 | ]
|
68 | 68 | },
|
69 | 69 | {
|
|
79 | 79 | "cell_type": "markdown",
|
80 | 80 | "metadata": {},
|
81 | 81 | "source": [
|
82 |
| - "**Gotcha** - Python (and many other langauges) start counting from 0." |
| 82 | + "**Important**: Python (and many other langauges) start counting from 0. They are **zero-indexed**." |
83 | 83 | ]
|
84 | 84 | },
|
85 | 85 | {
|
|
106 | 106 | "source": [
|
107 | 107 | "## You can slice strings using `[ : ]`\n",
|
108 | 108 | "\n",
|
109 |
| - "* if you want a range (or \"slice\") of a sequence, you get everything *before* the second index:" |
| 109 | + "* If you want a range (or \"slice\") of a sequence, you get everything *before* the second index:" |
110 | 110 | ]
|
111 | 111 | },
|
112 | 112 | {
|
|
131 | 131 | "cell_type": "markdown",
|
132 | 132 | "metadata": {},
|
133 | 133 | "source": [
|
134 |
| - "* You can see some of the logic for this when we consider implicit indices." |
| 134 | + "* You can see some of the logic for this when we consider implicit indices. If we want to start from the beginning of the string, or get all values until the end, we can just omit one end of the slice." |
135 | 135 | ]
|
136 | 136 | },
|
137 | 137 | {
|
|
156 | 156 | "cell_type": "markdown",
|
157 | 157 | "metadata": {},
|
158 | 158 | "source": [
|
159 |
| - "## String Have Methods" |
| 159 | + "## Strings have built-in methods" |
160 | 160 | ]
|
161 | 161 | },
|
162 | 162 | {
|
163 | 163 | "cell_type": "markdown",
|
164 | 164 | "metadata": {},
|
165 | 165 | "source": [
|
166 | 166 | "* There are other operations defined on string data. These are called **string [methods](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#method)**. \n",
|
167 |
| - "* IPython lets you do tab-completion after a dot ('.') to see what methods an [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) (i.e., a defined variable) has to offer. Try it now!" |
| 167 | + "* Jupyter lets you do tab-completion after a dot ('.') to see what methods an [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) (i.e., a defined variable) has to offer. Try it now!" |
168 | 168 | ]
|
169 | 169 | },
|
170 | 170 | {
|
|
180 | 180 | "cell_type": "markdown",
|
181 | 181 | "metadata": {},
|
182 | 182 | "source": [
|
183 |
| - "* Let's look at the upper method. What does it do? Lets take a look at the documentation. IPython lets us do this with a question mark ('?') before *or* after an object (again, a defined variable)." |
| 183 | + "* Let's look at the upper method. What does it do? Lets take a look at the documentation. Jupyter lets us do this with a question mark ('?') before *or* after an object (again, a defined variable)." |
184 | 184 | ]
|
185 | 185 | },
|
186 | 186 | {
|
187 | 187 | "cell_type": "code",
|
188 | 188 | "execution_count": null,
|
189 |
| - "metadata": { |
190 |
| - "collapsed": true |
191 |
| - }, |
| 189 | + "metadata": {}, |
192 | 190 | "outputs": [],
|
193 | 191 | "source": [
|
194 | 192 | "str.upper?"
|
|
198 | 196 | "cell_type": "markdown",
|
199 | 197 | "metadata": {},
|
200 | 198 | "source": [
|
201 |
| - "So we can use it to upper-caseify a string. " |
| 199 | + "We can use this function to convert a string to upper case." |
202 | 200 | ]
|
203 | 201 | },
|
204 | 202 | {
|
|
216 | 214 | "source": [
|
217 | 215 | "You have to use the parenthesis at the end because upper is a method of the string class.\n",
|
218 | 216 | "\n",
|
219 |
| - "Don't forget, simply calling the method does not change the original variable, you must reassign the variable:" |
| 217 | + "Don't forget: simply calling the method does not change the original variable. You must reassign the variable:" |
220 | 218 | ]
|
221 | 219 | },
|
222 | 220 | {
|
|
267 | 265 | "source": [
|
268 | 266 | "## Challenge 1: Write your name\n",
|
269 | 267 | "\n",
|
270 |
| - "1. Make two string variables, one with your first name and one with your last name.\n", |
| 268 | + "1. Create two string variables, one with your first name and one with your last name.\n", |
271 | 269 | "2. Concatenate both strings to form your full name and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it to a variable.\n",
|
272 | 270 | "3. Assign a new variable that has your full name in all upper case.\n",
|
273 | 271 | "4. Slice that string to get your first name again."
|
|
276 | 274 | {
|
277 | 275 | "cell_type": "code",
|
278 | 276 | "execution_count": null,
|
279 |
| - "metadata": { |
280 |
| - "collapsed": true |
281 |
| - }, |
| 277 | + "metadata": {}, |
282 | 278 | "outputs": [],
|
283 | 279 | "source": []
|
284 | 280 | },
|
285 | 281 | {
|
286 | 282 | "cell_type": "markdown",
|
287 | 283 | "metadata": {},
|
288 | 284 | "source": [
|
289 |
| - "## Challenge 2: Try seeing what the following string methods do:\n", |
| 285 | + "## Challenge 2: String methods\n", |
| 286 | + "\n", |
| 287 | + "Create a string variable, and apply the following methods to it to see what they do:\n", |
290 | 288 | "\n",
|
291 |
| - " * `split`\n", |
292 |
| - " * `join`\n", |
293 |
| - " * `replace`\n", |
294 |
| - " * `strip`\n", |
295 |
| - " * `find`" |
| 289 | + "* `split`\n", |
| 290 | + "* `join`\n", |
| 291 | + "* `replace`\n", |
| 292 | + "* `strip`\n", |
| 293 | + "* `find`" |
296 | 294 | ]
|
297 | 295 | },
|
298 | 296 | {
|
299 | 297 | "cell_type": "code",
|
300 | 298 | "execution_count": null,
|
301 |
| - "metadata": { |
302 |
| - "collapsed": true |
303 |
| - }, |
| 299 | + "metadata": {}, |
304 | 300 | "outputs": [],
|
305 | 301 | "source": []
|
306 | 302 | },
|
|
321 | 317 | {
|
322 | 318 | "cell_type": "code",
|
323 | 319 | "execution_count": null,
|
324 |
| - "metadata": { |
325 |
| - "collapsed": true |
326 |
| - }, |
| 320 | + "metadata": {}, |
327 | 321 | "outputs": [],
|
328 | 322 | "source": [
|
329 | 323 | "poem = '''Take this kiss upon the brow!\n",
|
|
377 | 371 | {
|
378 | 372 | "cell_type": "code",
|
379 | 373 | "execution_count": null,
|
380 |
| - "metadata": { |
381 |
| - "collapsed": true |
382 |
| - }, |
| 374 | + "metadata": {}, |
383 | 375 | "outputs": [],
|
384 | 376 | "source": []
|
385 | 377 | },
|
|
393 | 385 | {
|
394 | 386 | "cell_type": "code",
|
395 | 387 | "execution_count": null,
|
396 |
| - "metadata": { |
397 |
| - "collapsed": true |
398 |
| - }, |
| 388 | + "metadata": {}, |
399 | 389 | "outputs": [],
|
400 | 390 | "source": []
|
401 | 391 | },
|
|
405 | 395 | "collapsed": true
|
406 | 396 | },
|
407 | 397 | "source": [
|
408 |
| - "# Keypoints\n", |
| 398 | + "# Key Points\n", |
409 | 399 | "\n",
|
410 | 400 | "- Some mathematical operators can be used on strings\n",
|
411 |
| - "- Strings can be indexed, Python indexing always starts at 0!\n", |
| 401 | + "- Strings can be indexed. Python indexing always starts at 0!\n", |
412 | 402 | "- Strings, and other types, have their own methods, which are called using dots after the variable, and then the method name."
|
413 | 403 | ]
|
414 |
| - }, |
415 |
| - { |
416 |
| - "cell_type": "code", |
417 |
| - "execution_count": null, |
418 |
| - "metadata": { |
419 |
| - "collapsed": true |
420 |
| - }, |
421 |
| - "outputs": [], |
422 |
| - "source": [] |
423 | 404 | }
|
424 | 405 | ],
|
425 | 406 | "metadata": {
|
426 | 407 | "kernelspec": {
|
427 |
| - "display_name": "Python 3", |
| 408 | + "display_name": "Python 3 (ipykernel)", |
428 | 409 | "language": "python",
|
429 | 410 | "name": "python3"
|
430 | 411 | },
|
|
438 | 419 | "name": "python",
|
439 | 420 | "nbconvert_exporter": "python",
|
440 | 421 | "pygments_lexer": "ipython3",
|
441 |
| - "version": "3.6.3" |
| 422 | + "version": "3.9.7" |
442 | 423 | }
|
443 | 424 | },
|
444 | 425 | "nbformat": 4,
|
|
0 commit comments