diff --git a/practice_problems/ACT_340_3_1_Basic_String_Exercise.ipynb b/practice_problems/ACT_340_3_1_Basic_String_Exercise.ipynb index d903e3e..89af3bb 100644 --- a/practice_problems/ACT_340_3_1_Basic_String_Exercise.ipynb +++ b/practice_problems/ACT_340_3_1_Basic_String_Exercise.ipynb @@ -1,78 +1,60 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", + "metadata": { + "id": "DXWffeN5qdfN" + }, "source": [ "# String Practice Excercises- Basics\n", "\n", "A string is a series of charachters in Python. Anything inside of quotes is considreed a string. You can use single or double quotes around of a string." - ], - "metadata": { - "id": "DXWffeN5qdfN" - } + ] }, { "cell_type": "code", - "source": [ - "string1= \"This is a python string\"\n", - "string2= 'This is also a python string'" - ], + "execution_count": 4, "metadata": { "id": "8xHfXP_bYb7S" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "string1= \"This is a python string\"\n", + "string2= 'This is also a python string'" + ] }, { "cell_type": "markdown", - "source": [ - "This flexibility allows you to use quotes and apostrophes within our strings." - ], "metadata": { "id": "lnaUB0ILYkIt" - } + }, + "source": [ + "This flexibility allows you to use quotes and apostrophes within our strings." + ] }, { "cell_type": "code", - "source": [ - "string3= \"I told my friend, 'Python is my favorite programming language!'\"" - ], + "execution_count": 3, "metadata": { "id": "-mkroeSmYssJ" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "string3= \"I told my friend, 'Python is my favorite programming language!'\"" + ] }, { "cell_type": "markdown", - "source": [ - "All strings are part of the String Class in python. Because of this, we have access to all of the methods that exist within that Class. The way we use methods on string methods is called \"dot notation\".\n" - ], "metadata": { "id": "pskaACUjZQLu" - } + }, + "source": [ + "All strings are part of the String Class in python. Because of this, we have access to all of the methods that exist within that Class. The way we use methods on string methods is called \"dot notation\".\n" + ] }, { "cell_type": "code", - "source": [ - "name= 'sammi'\n", - "print(name.title())\n", - "print(name)" - ], + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -80,20 +62,27 @@ "id": "tis6wBmEZpIb", "outputId": "34429312-b84e-4811-930e-266c152228fa" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Sammi\n", "sammi\n" ] } + ], + "source": [ + "name= 'sammi'\n", + "print(name.title())\n", + "print(name)" ] }, { "cell_type": "markdown", + "metadata": { + "id": "0KsvVUnxZwpr" + }, "source": [ "In the example above, the variable \"name\" is referrring to the string object \"sammi\". We add the method(a function that belongs to a specific class) to the variable with a period. We are telling Python to perform the title method on the name variable. \n", "\n", @@ -102,13 +91,13 @@ "
\n", "You can find a list of all the methods that belong to the String Class here:\n", "https://www.w3schools.com/python/python_ref_string.asp" - ], - "metadata": { - "id": "0KsvVUnxZwpr" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "j2X6ijE-bKVj" + }, "source": [ "**Problem 1**\n", "\n", @@ -124,40 +113,46 @@ "\n", "\n", "\n" - ], - "metadata": { - "id": "j2X6ijE-bKVj" - } + ] }, { "cell_type": "code", - "source": [], + "execution_count": 8, "metadata": { "id": "JqXc8HG1ceRd" }, - "execution_count": null, - "outputs": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EVERYTHING IS AN OBJECT IN PYTHON!!!\n", + "everything is an object in python!!!\n", + "Everything is an object in python!!!\n" + ] + } + ], + "source": [ + "var1=\"Everything is an Object in Python!!!\"\n", + "print(var1.upper())\n", + "print(var1.lower())\n", + "print(var1.capitalize()) #capitalizes the first letter then lowers the other capitalized in string" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "i5dXWSIJcpa4" + }, "source": [ "# Using Variables in Strings\n", "\n", "There are often times you will want to use a variabe inside of a string. There are a few ways to do this, but the most common way is to use f-strings. " - ], - "metadata": { - "id": "i5dXWSIJcpa4" - } + ] }, { "cell_type": "code", - "source": [ - "first_name= \"john\"\n", - "last_name= \"doe\"\n", - "full_name= f\"{first_name} {last_name}\"\n", - "\n", - "print(full_name)" - ], + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -165,61 +160,69 @@ "id": "isIejAGTvySa", "outputId": "b0ead689-3818-4851-e418-6d63103f87f9" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ - "john doe\n" + " john doe 2\n" ] } + ], + "source": [ + "first_name= \"john\"\n", + "last_name= \"doe\"\n", + "number= 2\n", + "full_name= f\"{first_name:>9} {last_name} {number}\" #defualts to whitespace so dont need the s in :>9s\n", + "print(full_name)" ] }, { "cell_type": "markdown", + "metadata": { + "id": "OXA7NMvuwH3p" + }, "source": [ "The \"f\" stands for format because Python formats the string by replacing the name of any variable in braces with its value. It is placed before the first quotation mark in the string you want to format.\n", "\n", "
\n", "
\n", "We can also use our string methods inside of our f-strings." - ], - "metadata": { - "id": "OXA7NMvuwH3p" - } + ] }, { "cell_type": "code", - "source": [ - "print(f\"Hello, {full_name.title()}\")" - ], + "execution_count": 17, "metadata": { "id": "YaDYDkegwGzg" }, - "execution_count": null, - "outputs": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, John Doe 2\n" + ] + } + ], + "source": [ + "print(f\"Hello, {full_name.title()}\")" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "97eInbx005yz" + }, "source": [ "# Stripping Whitespace\n", "\n", "In Python, the string \"animal\" and \"animal \" are not equal. The whitespace becomes especially important when checking for the length of a string or comparing two strings. The string class has methods to help us remove whitespace." - ], - "metadata": { - "id": "97eInbx005yz" - } + ] }, { "cell_type": "code", - "source": [ - "phrase= \" reindeer games \"\n", - "\n", - "print(phrase.rstrip())\n", - "print(phrase.lstrip())\n", - "print(phrase.strip())" - ], + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -227,30 +230,39 @@ "id": "AbOuq4f51j91", "outputId": "29cbe0f7-a0ae-4ea5-a53e-e5de73d1f14e" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ " reindeer games\n", "reindeer games \n", "reindeer games\n" ] } + ], + "source": [ + "phrase= \" reindeer games \"\n", + "\n", + "print(phrase.rstrip()) #Returns a right trim version of the string\n", + "print(phrase.lstrip()) #Returns a left trim version of the string\n", + "print(phrase.strip()) #Returns a trimmed version of the string" ] }, { "cell_type": "markdown", - "source": [ - "A real-world scenario removing whitespace is to clean the values from user input or from data sets." - ], "metadata": { "id": "-Y3L20_R787R" - } + }, + "source": [ + "A real-world scenario removing whitespace is to clean the values from user input or from data sets." + ] }, { "cell_type": "markdown", + "metadata": { + "id": "1pVTqM3Z8ZbM" + }, "source": [ "**Problem 2**\n", "\n", @@ -260,22 +272,35 @@ "\n", "\"Michael Scott once said, \"I'm not superstitious but I am a little stitious.\"\n", "\n" - ], - "metadata": { - "id": "1pVTqM3Z8ZbM" - } + ] }, { "cell_type": "code", - "source": [], + "execution_count": 23, "metadata": { "id": "2nhPYKoCdrGF" }, - "execution_count": null, - "outputs": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Michael Scott once said, 'I'm not superstitious but I am a little stitious'\n" + ] + } + ], + "source": [ + "name= \"Michael Scott\"\n", + "quote= \"I'm not superstitious but I am a little stitious\"\n", + "message= f'{name} once said, \"{quote}\"'\n", + "print(message)" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "ULlgyjJ6dsmq" + }, "source": [ "# String Indexing and Slicing\n", "\n", @@ -284,20 +309,11 @@ "Like the list data type that has items that correspond to an index number, each of a string’s characters also correspond to an index number, starting with the index number 0.\n", "\n", "If we want to access a particular index in a string we can do this using square brackets.\n" - ], - "metadata": { - "id": "ULlgyjJ6dsmq" - } + ] }, { "cell_type": "code", - "source": [ - "course= \"Per Scholas\"\n", - "\n", - "print(course[1])\n", - "print(course[3])\n", - "print(course[-5])" - ], + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -305,32 +321,41 @@ "id": "OsNrMbApfSNs", "outputId": "6fb661d9-25ac-4f3a-f51b-96933dc0e5f0" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "e\n", " \n", "h\n" ] } + ], + "source": [ + "course= \"Per Scholas\"\n", + "\n", + "print(course[1])\n", + "print(course[3])\n", + "print(course[-5])" ] }, { "cell_type": "markdown", + "metadata": { + "id": "1uFolkBjgJ_2" + }, "source": [ "As you can see from the example above, we can access indexes by positive or negative integers in side the square bracket.\n", "\n", "The positive integers start from the left at the 0 index and negative integers start from the right at -1(which denotes the last charachter in a string)." - ], - "metadata": { - "id": "1uFolkBjgJ_2" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "jutJVjmvhf9X" + }, "source": [ "Slicing in Python is a feature that enables accessing parts of the sequence. In slicing a string, we create a substring, which is essentially a string that exists within another string. \n", "\n", @@ -346,21 +371,11 @@ "end : We provide the end index(this is not included in substring).\n", "\n", "step : It is an optional argument that determines the increment between each index for slicing." - ], - "metadata": { - "id": "jutJVjmvhf9X" - } + ] }, { "cell_type": "code", - "source": [ - "string1= \"Slicing strings is easy!\"\n", - "\n", - "print(string1[:3])\n", - "print(string1[1: 6: 2])\n", - "print(string1[-1: -8: -2])\n", - "print(string1[: : -1])" - ], + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -368,11 +383,10 @@ "id": "mem-fifMh16J", "outputId": "d745d629-ee31-47ec-d8f3-c1aac6898817" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Sli\n", "lcn\n", @@ -380,10 +394,21 @@ "!ysae si sgnirts gnicilS\n" ] } + ], + "source": [ + "string1= \"Slicing strings is easy!\"\n", + "\n", + "print(string1[:3])\n", + "print(string1[1: 6: 2])\n", + "print(string1[-1: -8: -2])\n", + "print(string1[: : -1])" ] }, { "cell_type": "markdown", + "metadata": { + "id": "SUnB0O91jA1k" + }, "source": [ "**Problem 3**\n", "\n", @@ -406,19 +431,39 @@ "\n", "\n", "\n" - ], - "metadata": { - "id": "SUnB0O91jA1k" - } + ] }, { "cell_type": "code", - "source": [], + "execution_count": null, "metadata": { "id": "ePbaxhL2kPwP" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "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.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}