Skip to content

Commit

Permalink
team teach 3.4 string operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypernova101 committed Sep 25, 2024
1 parent 5da8866 commit 8eab36d
Showing 1 changed file with 286 additions and 0 deletions.
286 changes: 286 additions & 0 deletions _notebooks/Foundation/2024-09-24-team_teach.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
{
"cells": [
{
"cell_type": "raw",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"toc: true\n",
"layout: post\n",
"type: hacks\n",
"courses: { compsci: {week: 14} }\n",
"title: String Operations Notebook\n",
"description: 3.4 Team Teach String Operations\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What are String Operations?\n",
" - String Operations allow us to manipulate and work with text-based data\n",
" - NEED to know for handling input/output (big in algorithmic coding), formatting, and data processing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Measuring String Length\n",
" - The len() function in Python allows us to find the length of a string\n",
" - Use .length in JavaScript to do this as well"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len(\"Hello\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Determine number of characters in string (eg for validation of password length)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Case Convertion\n",
" - Uppercase: Convert to uppercase using .upper() in Python\n",
" - Lowercase: Convert to lowercase using .lower() in Python"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HELLO\n",
"hello\n"
]
}
],
"source": [
"print(\"hello\".upper())\n",
"print(\"HELLO\".lower())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Useful for standardizing text inputs, like making email addresses case-insensitive"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Slicing\n",
" - Through string slicing, we can access a part of the string using indexes\n",
" - Each character in a string gets assigned a index, starting from 0\n",
" - Syntax is [startindex:endindex]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n"
]
}
],
"source": [
"print(\"Hello World\"[0:5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Extract substrings, like the first word from a sentence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding Substrings\n",
" - Searches for a substring and returns its position within a overlaying string\n",
" - .find() in Python"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"print(\"Hello World\".find(\"World\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Helpful for parsing text or finding key words"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Replacing Substrings\n",
" - Can replace different parts of a string with something else\n",
" - .replace() in Python"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Mihir\n"
]
}
],
"source": [
"print(\"Hello World\".replace(\"World\", \"Mihir\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Useful for replacing specific parts of text without having to re initiate the whole thing again"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Splitting Strings\n",
" - Splits a string into a list of substrings based on a delimiter (most commonly a space or comma)\n",
" - .split() in Python"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['apple', 'banana', 'grape']\n"
]
}
],
"source": [
"print(\"apple,banana,grape\".split(\",\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Parse through CSV files or processing large lists of items"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Joining Strings\n",
" - Basically opposite of splitting strings\n",
" - .join() in Python\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple,banana,grape\n"
]
}
],
"source": [
"print(\",\".join(['apple', 'banana', 'grape']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Use Case: Combine a list of strings into a single string, helpful for reformatting content"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 8eab36d

Please sign in to comment.