diff --git a/Intermediate.ipynb b/Intermediate.ipynb index 6edb3ef..60b62f7 100644 --- a/Intermediate.ipynb +++ b/Intermediate.ipynb @@ -217,7 +217,7 @@ "editable": false }, "source": [ - "# **1.** Recap" + "# 1. Recap" ] }, { @@ -4246,6 +4246,62 @@ "# 7. Advanced string manipulation" ] }, + { + "cell_type": "markdown", + "id": "e34aedb6", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## String Methods - Working with String Objects" + ] + }, + { + "cell_type": "markdown", + "id": "ffef8f84", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "In Python, strings are objects, and objects have **methods** - functions that belong to and operate on that object.\n", + "\n", + "**Syntax**: `string_object.method_name(arguments)`\n", + "\n", + "Key characteristics:\n", + "- Methods are called using _dot notation_: `variable.method()`\n", + "- String methods return a **new string** (strings are immutable)\n", + "- The original string is never modified\n", + "- You must assign the result if you want to keep it\n", + "\n", + "**Example:**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5bd4280", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "text = \"hello world\"\n", + "result = text.upper() # Creates new string\n", + "print(text) # Still \"hello world\" \n", + "print(result) # \"HELLO WORLD\"" + ] + }, { "cell_type": "markdown", "id": "c9016a2e-dee1-4030-a1a0-8e519a84be6f", @@ -4257,10 +4313,11 @@ "editable": false }, "source": [ - "* Adjusting case\n", - "* Formatting strings\n", - " - _Note_: Modification requires assignment, because these functions return a copy, not modifying the original string\n", - "* Quering the existence, replacing, splitting" + "Common categories of string methods we'll explore:\n", + "- **Finding/searching**: `find()`, `index()`\n", + "- **Checking**: `startswith()`, `endswith()`\n", + "- **Transforming**: `replace()`, `upper()`, `lower()`, `capitalize()`, `strip()`\n", + "- **Splitting/joining**: `split()`, `join()`" ] }, { @@ -4274,33 +4331,28 @@ "editable": false }, "source": [ - "## Finding values \n", - "* `find()` and `index()` both return index of a substring,\n", - " - `index()` raises a `ValueError` exception when not found (_exception handling_)\n", - " - `find()` returns `-1` when a values was not found\n" + "## Finding/searching string methods" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "0eab0a2d-eea9-4c33-b700-56eb293ef28b", + "cell_type": "markdown", + "id": "a4fa9b5b", "metadata": { + "editable": false, "slideshow": { "slide_type": "" - }, - "tags": [] + } }, - "outputs": [], "source": [ - "line = \"the quick brown fox jumped over a lazy dog\"\n", - "print(line.find('fox'))\n", - "print(line.index('fox'))" + "These methods help you locate substrings within a string, returning their position or checking for their presence:\n", + "* `index()`\n", + "* `find()`" ] }, { "cell_type": "code", "execution_count": null, - "id": "134c6eba-8897-4ee8-a6e5-f3740a0a4d21", + "id": "0eab0a2d-eea9-4c33-b700-56eb293ef28b", "metadata": { "slideshow": { "slide_type": "" @@ -4309,13 +4361,15 @@ }, "outputs": [], "source": [ - "print(line.find('wombat'))" + "line = \"the quick brown fox jumped over a lazy dog\"\n", + "print(line.find('fox'))\n", + "print(line.index('fox'))" ] }, { "cell_type": "code", "execution_count": null, - "id": "fc5a28ef-8a31-434a-a653-1662cc274945", + "id": "134c6eba-8897-4ee8-a6e5-f3740a0a4d21", "metadata": { "slideshow": { "slide_type": "" @@ -4324,10 +4378,7 @@ }, "outputs": [], "source": [ - "try:\n", - " print(line.index('wombat'))\n", - "except ValueError:\n", - " print(\"A wombat isn't mentioned in the text\")" + "print(line.find('wombat'))" ] }, { @@ -4341,7 +4392,7 @@ "editable": false }, "source": [ - "## Checking conditions on strings" + "## Checking string methods" ] }, { @@ -4355,7 +4406,9 @@ "editable": false }, "source": [ - "* Checking whether a string starts or ends a certain way is really common and easy" + "These methods return boolean values (`True` or `False`) to verify if a string matches certain patterns or criteria:\n", + "* `startswith(substring)`\n", + "* `endswith(substring)`" ] }, { @@ -4399,13 +4452,13 @@ "id": "e040bf97-a6bd-493e-b047-2041b5a99131", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [], "editable": false }, "source": [ - "* The canonical way to search a string (if not interested in the index):" + "The canonical way to search a string (if not interested in the index):" ] }, { @@ -4435,7 +4488,25 @@ "editable": false }, "source": [ - "## Replacing a value:" + "## Transforming string methods" + ] + }, + { + "cell_type": "markdown", + "id": "80b12b8b", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "These methods create modified versions of strings by replacing content, changing case, or removing unwanted characters. _**Remember**: strings are immutable, so these methods always return a new string._\n", + "* `replace(old, new)`\n", + "* `upper()`\n", + "* `lower()`\n", + "* `capitalize()`\n", + "* `strip()`" ] }, { @@ -4455,20 +4526,6 @@ "print(line.replace('brown', 'red'))" ] }, - { - "cell_type": "markdown", - "id": "bb4faee5-8c3f-4919-acbe-1b7b2cf7b700", - "metadata": { - "slideshow": { - "slide_type": "slide" - }, - "tags": [], - "editable": false - }, - "source": [ - "## Bring all words to a common case" - ] - }, { "cell_type": "code", "execution_count": null, @@ -4488,20 +4545,6 @@ "print(arc_update.capitalize())" ] }, - { - "cell_type": "markdown", - "id": "7a25cc7f-6040-4c03-ac38-b5f9a7baef41", - "metadata": { - "slideshow": { - "slide_type": "slide" - }, - "tags": [], - "editable": false - }, - "source": [ - "## Removing white space" - ] - }, { "cell_type": "code", "execution_count": null, @@ -4531,7 +4574,22 @@ "editable": false }, "source": [ - "## Extracting/concatenating the individual words or parts" + "## Splitting/joining string methods" + ] + }, + { + "cell_type": "markdown", + "id": "71a4546c", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "These methods allow you to break strings apart into lists of substrings, or combine lists of strings into a single string. They're particularly useful for parsing text data or formatting output:\n", + "* `split(separator)` - splits string into a list at each occurrence of separator (defaults to whitespace)\n", + "* `join(iterable)` - joins elements of an iterable into a single string with the string as separator" ] }, { @@ -4566,7 +4624,7 @@ "editable": false }, "source": [ - "The operation in the other direction is `a_string.join()` where `a_string` is placed between every string of a list" + "The `join` operation is `a_string.join()` where `a_string` is placed between every string of a list" ] }, { @@ -5848,7 +5906,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.13.0" } }, "nbformat": 4, diff --git a/Intermediate_full.ipynb b/Intermediate_full.ipynb index ed93204..2ab66cd 100644 --- a/Intermediate_full.ipynb +++ b/Intermediate_full.ipynb @@ -205,7 +205,7 @@ "tags": [] }, "source": [ - "# **1.** Recap" + "# 1. Recap" ] }, { @@ -3131,7 +3131,9 @@ } }, "source": [ - "_Note_: the data structure returned by `dict.items()` is of type `dict_items`,\n", + "Speaker notes:\n", + "\n", + "The data structure returned by `dict.items()` is of type `dict_items`,\n", "which is an iterable view object displaying the dictionary’s (key, value) pairs" ] }, @@ -3686,7 +3688,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", " - We are using indented blocks again\n", " - All statements in the try block will be executed one by one\n", " - If one fails, the interpreter checks whether an except block with a matching exception type exists and execute the code in there\n", @@ -3742,7 +3744,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", "- Do __not__ write out the function again, modify the original function to look like the result function\n", "- Demonstrate that you can catch the exception with a bare except\n", "- Change the function to use the `ZeroDivisionError` explicitely, to avoid catching errors we do not mean to catch" @@ -3811,7 +3813,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", "- Do __not__ write out the function again, modify the original function to look like the result function\n", "- Using the from keyword we can add an exception to the error stack.\n", "- This can be used to give additional information, that makes the error source in the input easily traceable" @@ -3868,7 +3870,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", "- Do __not__ write out the function again, modify the original function to look like the result function\n", "- It is good practice to have these checks early in a function. Nobody likes to be 10h into a script execution to then fail a validation" ] @@ -3955,7 +3957,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", "- Do __not__ write out the function again, modify the original function to look like the result function\n", "- Finally is often used in clean up operations." ] @@ -4267,7 +4269,7 @@ "tags": [] }, "source": [ - "Notes:\n", + "Speaker notes:\n", "- In JupyterLite, logging output is suppressed by default unless you explicitly configure a handler." ] }, @@ -4372,6 +4374,62 @@ "# 7. Advanced string manipulation" ] }, + { + "cell_type": "markdown", + "id": "e34aedb6", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## String Methods - Working with String Objects" + ] + }, + { + "cell_type": "markdown", + "id": "ffef8f84", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "In Python, strings are objects, and objects have **methods** - functions that belong to and operate on that object.\n", + "\n", + "**Syntax**: `string_object.method_name(arguments)`\n", + "\n", + "Key characteristics:\n", + "- Methods are called using _dot notation_: `variable.method()`\n", + "- String methods return a **new string** (strings are immutable)\n", + "- The original string is never modified\n", + "- You must assign the result if you want to keep it\n", + "\n", + "**Example:**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5bd4280", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "text = \"hello world\"\n", + "result = text.upper() # Creates new string\n", + "print(text) # Still \"hello world\" \n", + "print(result) # \"HELLO WORLD\"" + ] + }, { "cell_type": "markdown", "id": "c9016a2e-dee1-4030-a1a0-8e519a84be6f", @@ -4382,10 +4440,11 @@ "tags": [] }, "source": [ - "* Adjusting case\n", - "* Formatting strings\n", - " - _Note_: Modification requires assignment, because these functions return a copy, not modifying the original string\n", - "* Quering the existence, replacing, splitting" + "Common categories of string methods we'll explore:\n", + "- **Finding/searching**: `find()`, `index()`\n", + "- **Checking**: `startswith()`, `endswith()`\n", + "- **Transforming**: `replace()`, `upper()`, `lower()`, `capitalize()`, `strip()`\n", + "- **Splitting/joining**: `split()`, `join()`" ] }, { @@ -4398,10 +4457,22 @@ "tags": [] }, "source": [ - "## Finding values \n", - "* `find()` and `index()` both return index of a substring,\n", - " - `index()` raises a `ValueError` exception when not found (_exception handling_)\n", - " - `find()` returns `-1` when a values was not found\n" + "## Finding/searching string methods" + ] + }, + { + "cell_type": "markdown", + "id": "a4fa9b5b", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "These methods help you locate substrings within a string, returning their position or checking for their presence:\n", + "* `index()`\n", + "* `find()`" ] }, { @@ -4437,21 +4508,17 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "id": "fc5a28ef-8a31-434a-a653-1662cc274945", + "cell_type": "markdown", + "id": "714fc569", "metadata": { "slideshow": { - "slide_type": "" - }, - "tags": [] + "slide_type": "notes" + } }, - "outputs": [], "source": [ - "try:\n", - " print(line.index('wombat'))\n", - "except ValueError:\n", - " print(\"A wombat isn't mentioned in the text\")" + "Speaker notes:\n", + "\n", + "The key difference is how they handle missing substrings - `find()` returns `-1` while `index()` raises an exception:" ] }, { @@ -4464,7 +4531,7 @@ "tags": [] }, "source": [ - "## Checking conditions on strings" + "## Checking string methods" ] }, { @@ -4477,7 +4544,24 @@ "tags": [] }, "source": [ - "* Checking whether a string starts or ends a certain way is really common and easy" + "These methods return boolean values (`True` or `False`) to verify if a string matches certain patterns or criteria:\n", + "* `startswith(substring)`\n", + "* `endswith(substring)`" + ] + }, + { + "cell_type": "markdown", + "id": "6ac1c80f", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "Speaker notes:\n", + "\n", + "These methods are case-sensitive." ] }, { @@ -4521,12 +4605,12 @@ "id": "e040bf97-a6bd-493e-b047-2041b5a99131", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [] }, "source": [ - "* The canonical way to search a string (if not interested in the index):" + "The canonical way to search a string (if not interested in the index):" ] }, { @@ -4555,37 +4639,42 @@ "tags": [] }, "source": [ - "## Replacing a value:" + "## Transforming string methods" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "292d8b56-1142-4927-9320-9c6331f5a962", + "cell_type": "markdown", + "id": "80b12b8b", "metadata": { + "editable": true, "slideshow": { "slide_type": "" - }, - "tags": [] + } }, - "outputs": [], "source": [ - "line = \"the quick brown fox jumped over a lazy dog\"\n", - "print(line)\n", - "print(line.replace('brown', 'red'))" + "These methods create modified versions of strings by replacing content, changing case, or removing unwanted characters. _**Remember**: strings are immutable, so these methods always return a new string._\n", + "* `replace(old, new)`\n", + "* `upper()`\n", + "* `lower()`\n", + "* `capitalize()`\n", + "* `strip()`" ] }, { - "cell_type": "markdown", - "id": "bb4faee5-8c3f-4919-acbe-1b7b2cf7b700", + "cell_type": "code", + "execution_count": null, + "id": "292d8b56-1142-4927-9320-9c6331f5a962", "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "" }, "tags": [] }, + "outputs": [], "source": [ - "## Bring all words to a common case" + "line = \"the quick brown fox jumped over a lazy dog\"\n", + "print(line)\n", + "print(line.replace('brown', 'red'))" ] }, { @@ -4607,19 +4696,6 @@ "print(arc_update.capitalize())" ] }, - { - "cell_type": "markdown", - "id": "7a25cc7f-6040-4c03-ac38-b5f9a7baef41", - "metadata": { - "slideshow": { - "slide_type": "slide" - }, - "tags": [] - }, - "source": [ - "## Removing white space" - ] - }, { "cell_type": "code", "execution_count": null, @@ -4648,7 +4724,22 @@ "tags": [] }, "source": [ - "## Extracting/concatenating the individual words or parts" + "## Splitting/joining string methods" + ] + }, + { + "cell_type": "markdown", + "id": "71a4546c", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + } + }, + "source": [ + "These methods allow you to break strings apart into lists of substrings, or combine lists of strings into a single string. They're particularly useful for parsing text data or formatting output:\n", + "* `split(separator)` - splits string into a list at each occurrence of separator (defaults to whitespace)\n", + "* `join(iterable)` - joins elements of an iterable into a single string with the string as separator" ] }, { @@ -4682,7 +4773,7 @@ "tags": [] }, "source": [ - "The operation in the other direction is `a_string.join()` where `a_string` is placed between every string of a list" + "The `join` operation is `a_string.join()` where `a_string` is placed between every string of a list" ] }, { @@ -5886,7 +5977,7 @@ "tags": [] }, "source": [ - "Speaker notes\n", + "Speaker notes:\n", " - In this example, participants can be contacted via e-mail or Teams\n", " - The information we need for the two contact possibilities is completely independent\n", " - The specific behaviour is used by the class, but the details are not important for the class\n", @@ -6165,7 +6256,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.13.0" } }, "nbformat": 4,