Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 119 additions & 61 deletions Intermediate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"editable": false
},
"source": [
"# <ins>**1.**</ins> Recap"
"# <ins>1.</ins> Recap"
]
},
{
Expand Down Expand Up @@ -4246,6 +4246,62 @@
"# <ins>7.</ins> 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",
Expand All @@ -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()`"
]
},
{
Expand All @@ -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": ""
Expand All @@ -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": ""
Expand All @@ -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'))"
]
},
{
Expand All @@ -4341,7 +4392,7 @@
"editable": false
},
"source": [
"## Checking conditions on strings"
"## Checking string methods"
]
},
{
Expand All @@ -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)`"
]
},
{
Expand Down Expand Up @@ -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):"
]
},
{
Expand Down Expand Up @@ -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()`"
]
},
{
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -5848,7 +5906,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down
Loading