{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "execute:\n", " freeze: auto\n", " cache: true\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings\n", "\n", "[Jupyter Notebook](https://lancejnelson.github.io/PH135/jupyter/strings.ipynb)\n", "\n", "\n", "Another commonly-used type of data is a string of characters known simply as *strings*. Strings can contain a variety of characters including letters, numbers, and symbols. \n", "\n", "## Creating Strings\n", "\n", "Strings are created by placing the sequence of characters in single (or double) quotes." ] }, { "cell_type": "code", "metadata": {}, "source": [ "text = \"some text\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can also be created by converting a float or an integer into a string using the `str()` function." ] }, { "cell_type": "code", "metadata": {}, "source": [ "text = str(4.5)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One common error made when working with strings is to attempt to perform math with them. Python will not perform math with strings because it sees them as a series of characters and nothing more. In the cell below, we attempt to perform math with some strings." ] }, { "cell_type": "code", "metadata": {}, "source": [ "#| echo: true\n", "#| eval: false\n", "\n", "a = \"4\"\n", "b = \"2\"\n", "c = 5\n", "\n", "d = a + b\n", "e = a * b\n", "f = b * c" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Use print statements in the cell above to determine what happens when you add two strings together. (Ancillary question: Can you subtract strings?)\n", ">2. Use print statements in the cell above to determine what happens when you multiply two strings.\n", ">3. Use print statements in the cell above to determine what happens when you multiply a string and an integer.\n", ">4. What do you think will happen if you multiplied a string and a float? Guess and then see if you're right.\n", ">5. Add comments for you to refer back to later." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to know the length of a string, you can use the `len()` function" ] }, { "cell_type": "code", "metadata": {}, "source": [ "text = \"some text\"\n", "len(text)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The length of the string above is 9 because a space is a valid character.\n", "\n", "## Displaying text and numbers together\n", "\n", "You have been using `print()` statements quite a lot lately (hopefully) but you probably haven't printed text and numbers together. To display both text and numbers in the same message, there are several options. The first is to just put multiple variables into the `print()` function, separating them with commas." ] }, { "cell_type": "code", "metadata": {}, "source": [ "g = 9.8\n", "unit = \"m/s^2\"\n", "print(g,unit)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another option is to convert the number to a string and then \"add\" it to the other string. This creates a single string as an argument to the `print()` function." ] }, { "cell_type": "code", "metadata": {}, "source": [ "g = 9.8\n", "unit = \"m/s^2\"\n", "print(str(g) + unit)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice the lack of space between the number and the unit. \"Adding\" the two strings smashed them together exactly as they were, no spaces added. You can insert multiple numbers into a string using something called \"f\"-strings. (short for formatted strings). To construct an f-string, simply place an \"f\" in front of the string. Anytime you want to insert a number in your string, enclose it in curly braces." ] }, { "cell_type": "code", "metadata": {}, "source": [ "v = 5.0\n", "c = 3e8\n", "print(f\"The speed of light is {c} and the speed of my car is {v}\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That’s a clever way to insert a numerical value into a string, but the value of the speed of light is still displaying too many digits. To specify how the number should be formatted place a `:` after the variable name followed by a formatting tag. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "v1 = 5.0\n", "v2 = 8.3\n", "c = 2.998e8\n", "n = 2\n", "\n", "print(f\"There are {n:d} cars traveling side by side. One car is traveling at {v1:4.2f} m/s and the other is traveling at {v2:4.2f} m/s. Those speeds are much smaller than the speed of light, which is {c:.2e} m/s\") " ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The structure of the stuff inside of the curly braces is `{variable:formatcode}`; `variable` holds the value to be displayed and `formatcode` indicates how the variable should be formatted when it is printed. The `f` in `:4.2f` indicates that the variable should be displayed as a float and the `4.2` indicates that four spaces should be allocated to display the number and no more than 2 numbers after the decimal should be displayed. A selection of some commonly-used format codes is given below.\n", "\n", "\n", "| format code| explanation|\n", "|------------|------------|\n", "|`{variable}` | Use the default format for the data type.|\n", "|`{variable:4d}` | Display as an integer, allocating 4 spaces for it.|\n", "|`{variable:.4f}` | Display as a float, with four numbers after the decimal being displayed.|\n", "|`{variable:8.4f}` | Display as a float, allocating 8 total spaces and 4 numbers after the decimal place.|\n", "|`{variable:8.4e}` | Display using scientific notation, allocating 8 total spaces and 4 numbers after the decimal place.|\n", "|`{variable:6s}` | Display as a string, allocating 6 total spaces for it. If the string is longer than 6 spaces, it will display the entire string with no extra white space. If the string is shorter than 6 spaces, it will pad the string with whitespace until it is 6 spaces long.|\n", "\n", ": A summary of common format codes.\n", "\n", ">**_To Do:_**\n", ">\n", ">1. Modify the print statement above so that the float variables are given 8 total spaces with only 1 number after the decimal being displayed.\n", ">2. Modify the print statement above so that the speed of light is displayed with 3 numbers after the decimal place." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing and Slicing\n", "\n", " Accessing a piece (or slice) of a string is a common task in scientific computing. Often you will import data into Python from a text file and need to extract a portion of the file for later use in calculations. Indexing allows the user to extract a single element, or character, from a string. The key detail about indexing in Python is that *indices start from zero*. That means that the first character is index zero, the second character is index 1, and so on. For example, maybe a string contains the following amino acid sequence 'MSLFKIRMPE'. For this example, the indices are as follows:\n", " \n", "| **Characters**|M|S|L|F|K|I|R|M|P|E|\n", "| --------------- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- |\n", "| **Index** | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n", " \n", " \n", " \n", "\n", "To access a single character from a string, place the desired index in square brackets after the name of the string." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[0]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access the last character in a string you could do" ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPI\"\n", "seq[len(seq) - 1]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But this seems overly cumbersome. An easier approach is to index backwards. The string can be reverse indexed from the last character to the first using negative indices, starting with -1 as the last charcter." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[-1]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Access the 5th character in the peptide sequence above.\n", ">2. Access the character that is 3rd from the end in the peptide sequence above." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing only provides a single character, but it is common to want a series of characters from a string. *Slicing* allows us to grab a section of a string. Slicing is performed by specifying start and stop indices separated by a colon in the square brackets. One important detail worth mentioning: the character at the starting index is included in the slice while the character located at the final index *is not* included in the slice." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[0:5]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looking at the string, you notice that the character at location 5 (I) has been excluded from the slice. You can leave off the first number when slicing and the slice will start at the beginning of the string." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[:5]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use negative indices when slicing. This is especially helpful when you want to grab the last few characters in a string." ] }, { "cell_type": "code", "metadata": {}, "source": [ "file = \"data.txt\"\n", "ext = file[-3:]\n", "print(ext)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can adjust the step size in the slice. That is, we can ask for every other character in the string by setting a step size of 2. The structure of the slice is [start,stop,step]." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[0:8:2]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can omit the start and stop indices and Python will assume that you are slicing the entire string." ] }, { "cell_type": "code", "metadata": {}, "source": [ "seq = \"MSLFKIRMPE\"\n", "seq[::2]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String Methods\n", "\n", "A *method* is a function that works only with a specific type of object. String methods only work on strings, and they don't work on other types of objects, like floats or ints. If it helps you, you can just think of a method as a function. \n", "\n", "One example of a string method is the `capitalize()` function which returns a string with the first letter capitalized. To use a method (referred to as *calling* the method), the method name is appended to the variable you want it to operate on. For example, below is an Albert Einstein quote that needs capitalized." ] }, { "cell_type": "code", "metadata": {}, "source": [ "quote = \"i want to know God's thoughts. The rest are details.\"\n", "quote.capitalize()\n", "print(quote)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the original variable (quote) remains unchanged. This particular method does not change the value of the original string but rather returns a capitalized version of it. If we want to save the capitalized version, we can assign it to a new variable, or overwrite the original." ] }, { "cell_type": "code", "metadata": {}, "source": [ "quote = \"i want to know God's thoughts. The rest are details\"\n", "quote = quote.capitalize()\n", "print(quote)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the cell below you will find a list of commonly-used string methods." ] }, { "cell_type": "code", "metadata": {}, "source": [ "a = \"spdfgssfpggg\"\n", "a.capitalize()\n", "a.center(10)\n", "a.count(\"s\")\n", "a.find(\"d\")\n", "a.isalnum()\n", "a.isalpha()\n", "a.isdigit()\n", "a.lstrip(\"s\")\n", "a.rstrip(\"g\")\n", "a.split(\"s\")\n", "a.replace(\"sss\",\"ggg\")\n", "a.startswith(\"s\")\n", "a.endswith(\"p\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**_To Do:_**\n", ">\n", ">1. Use well-placed print statements to determine what each string method does.\n", ">2. Add comments next to each method for future reference.\n", ">" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Python Code Here!" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flash Cards\n", "1. What is the index value of the sixth character in a string?\n", "2. What happens when you \"add\" two strings together?\n", "3. What happens when you multiply two strings together?\n", "4. What happens when you multiply a string by an integer?\n", "5. What happens when you multiply a string by a float?\n", "6. What function will tell you how many characters are in a string?\n", "7. Give two ways that you could slice out the **first 4 characters in a string**.\n", "8. Give two ways that you could slice out the **last 4 characters in a string**.\n", "9. What string method would you use to see if a string contains only numbers and characters, but no symbols?\n", "10. What does the string method `split()` do?\n", "11. What does the string method `find()` do?\n", "12. Explain how to print a sentence with a variable inserted in the middle?\n", "13. What format code should be used to display a variable as a **float with 8 total spaces and 2 numbers after the decimal**?\n", "14. What format code should be used to display a variable in **scientific notation with 6 total spaces and 2 numbers after the decimal**?\n", "15. Where is the parable of the wheat and the tares found? \n", "\n", "\n", "\n", "## Exercises\n", "\n", "\n", "1. In the cell below you will find a string containing the name of a PNG file. (i.e. name always ends in \".png\"). \n", " i) Remove the \".png\" extension from the string **using a string method**.\n", " ii) Remove the \".png\" extension from the string **using slicing.**" ] }, { "cell_type": "code", "metadata": {}, "source": [ "file = \"physics.png\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. (Lightning Strike) The dielectric strength of air determines how likely it is for lightning to strike and this value varies with the humidity (moisture content) of the air. In the cell below, you will find a string that contains the dielectric strength of air for three different humidity levels. **Use string methods** to extract the three dielectric constant values. Then calculate \n", " 1. the difference between the largest and smallest values.\n", " 2. The average of the three values.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "dielectricBreakdownAir = \"dry:15847,moderate:8523,wet:7864\"\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Below you will find a string containing all experimentally known alloys (compounds formed by combining two or more elements on the periodic table) represented using the elements first discovered to exhibit that crystal structure (or \"prototypes\").\n", "\n", " 1. Use string methods to find the number of alloys containing Aluminum (symbol: Al).\n", " 2. Is there a crystal structure with the designation \"CaC2\" (Calcium Carbide) in the list? How about \"Fe2Ag\"?\n", " 3. Use string methods to calculate the percentage of the alloys listed that have gold in them (symbol Au). " ] }, { "cell_type": "code", "metadata": {}, "source": [ "alloys = \"Cu,W,Mg,betaSn,Indium,alphaAs,alphaHg,Cu,AuCu,CuPt,AB2,MoPt2,AB2,AB3,AB3,A2B2,AB3,A2B2,AB3,A2B2,Al3Ti,AuCu3,AB3,A2B2,W,W,gamma-IrV,CsCl,AB2,AB2,MoSi2,AB3,AB3,A2B2,AB3,A2B2,AB3,gamma-CuTi,AB3,A2B2,CuTi3,AlFe3/BiF3,NaTl,Mg,WC,AB3,CuTe,AuCd,A2B2,AB3,AB3,A2B2,AB5,A4B2,A4B2,A4B2,A2B4,A3B3,A3B3,AB5,A4B2,A4B2,A4B2,A2B4,A3B3,A3B3,AB5,A2B4,A2B4,AB5,A2B4,A2B4,A3B3,AB5,A2B4,A3B3,A2B4,A2B4,A3B3,MgZn2,Fe3C,Cu2Mg,Cr3Si,Ni3Sn,ZrSi2,CrB,ITl,FeSi,CdTi,InTh,SeTl,NaCl,BaAl4,CaCu5,ThMn12,Fe2P,Co2Si,Th3P4,AlB2,ZnS,ZnS,NiAs,Mn5Si3,Cr3Si,Cu2Sb,MoSi2,Al2Cu,Cd58Y13,Co3V,Pu3Al,CrFe,NbPd3,Ni3Ti,CaIn2,ThSi2,CaC2,AuBe5,W5Si3,La2O3,BFe,FeS2,Bi2Te3,Ti3Cu4,FeS2,CdI2,Cd3Y,Ni2In,Ni2Si,betaCu3Ti,Al3Zr,FeS2,GdSi2(1.4),MoNi4,CuZr2,alphaReO3,PbO/PbS,Indium,betaSn,alphaAs,CuPt7,NbNi8,V4Zn5,MgNi2,alphaGa,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sMg,Ag3[PO4],Ca3Al2O6,CaTiO3,Ce5Mo3O16,K2PtCl6,Cr9Fe16Ni7,CrFe18Ni8,AlCu2Mn,Eu2Ir2O7,Al2MgO4,Fe3W3C,CrFe12Ni3,CrFe4Ni3,CaFeO6Si2,Na2MgAlF7,Ca4Al6O16S,YBa2Cu3O7-x,alpha-RbPr[MoO4]2,CsPr[MoO4]2,KCNS,KAg[CO3],BaCr2Ru4O12,MgB2O(OH)6,C17FeO4Pt,Na4Ti2Si8O22[H2O]4,NaGdCu2F8,Ba2TiSi2O8,NaZn[OH]3,Ca2MgSi2O7,Cu2FeS4Sn,CaRbFe4As4,AsCuSiZr,Rb2TiCu2Se4,YbBaCo4O7,KAg(CN)2,pi-FeMg3Al8Si6,pi-FeMg3Al9Si5,Be3Al2Si6O18,MgB12H12[H2O]12,LiMgAuSn,Mg3B7ClO13,CrFe11MoNi3,CrFe2525Ni6,CuCrCl5[NH3]6,Co3Al2Si3O12,Cu8(Fe,Zn)3Sn2S12,BaCu4[VO][PO4]4,AsPh4CeS8P4Me8,NaCa3[CO3]2F3[H2O],Na3Co(CO3)2Cl,Na6Mg2(SO4)(CO3)4,Cs2ZnFe[CN]6,AB,A5B11,AB3,AB3,A3B13,A3B5,AB7,Cu2(Zn,Fe)SnS4\"\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. In the cell below, you will find a very long string containing a random sequence of numbers (spelled out) selected from the numbers 1-10. Use string methods to add all of the numbers up.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "numbersString = \"eight-six-four-ten-two-seven-five-four-three-ten-one-nine-seven-five-five-one-seven-nine-two-six-three-three-four-one-eight-five-five-ten-three-one-nine-three-nine-two-four-eight-three-nine-nine-four-eight-two-one-two-three-nine-five-six-three-seven-one-eight-four-seven-nine-three-seven-nine-five-eight-ten-two-two-four-ten-eight-four-three-ten-ten-three-two-ten-nine-five-eight-three-nine-eight-four-six-three-one-five-ten-two-one-one-three-three-five-ten-eight-six-five-six-three-one-three-seven-five-five-five-nine-four-one-eight-eight-eight-five-two-eight-two-two-three-five-nine-three-seven-seven-five-eight-ten-one-seven-one-nine-four-one-five-six-seven-three-three-nine-four-six-two-three-eight-eight-four-ten-two-six-one-five-five-nine-one-four-eight-six-six-four-five-five-two-four-one-two-five-eight-seven-seven-four-three-one-eight-four-three-one-two-ten-five-four-one-two-seven-nine-four-three-eight-four-six-ten-nine-two-four-eight-ten-two-two-nine-one-eight-three-seven-ten-two-three-ten-three-three-ten-six-one-ten-four-ten-nine-nine-six-six-one-two-nine-four-two-nine-nine-seven-two-nine-three-five-three-one-three-four-three-two-six-six-six-three-six-four-seven-two-ten-five-one-one-three-five-one-seven-six-two-four-one-five-two-seven-three-seven-five-five-two-one-ten-six-one-ten-eight-five-ten-ten-three-nine-two-seven-two-four-four-nine-three-two-three-two-ten-six-two-seven-two-four-eight-three-ten-two-four-three-nine-three-one-seven-ten-ten-three-five-two-four-eight-nine-five-ten-one-seven-eight-three-four-ten-one-nine-six-one-four-two-ten-eight-three-two-four-three-seven-three-nine-seven-four-five-one-nine-seven-two-eight-eight-six-three-five-three-six-five-four-five-one-nine-two-ten-four-three-three-five-ten-eight-one-two-five-seven-three-two-ten-six-four-six-nine-eight-three-eight-nine-one-two-eight-six-six-four-three-six-ten-two-three-ten-four-three-six-one-five-four-six-one-seven-five-five-eight-five-seven-six-nine-two-two-eight-six-three-four-seven-seven-eight-two-six-five-two-five-seven-three-seven-six-two-eight-eight-ten-nine-eight-six-eight-four-five-two-seven-four-six-one-eight-six-six-four-one-nine-three-seven-six-ten-four-five-one-one-seven-seven-eight-two-nine-seven-eight-six-six-seven-nine-three-one-one-nine-nine-seven-seven-three-one-six-four-ten-five-four-three-ten-nine-eight-ten-nine-ten-ten-one-seven-nine-two-one-seven-two-eight-four-five-five-three-three-two-six-eight-nine-four-four-one-six-six-five-two-two-six-four-two-ten-two-six-ten-two-five-four-one-eight-three-eight-nine-ten-seven-eight-five-six-one-seven-eight-seven-four-four-six-eight-six-five-ten-eight-eight-two-six-five-five-nine-six-ten-six-three-six-one-four-eight-one-two-ten-eight-four-seven-seven-nine-seven-four-two-five-two-eight-ten-seven-one-eight-six-five-five-nine-ten-four-three-eight-three-eight-five-ten-ten-six-eight-seven-one-seven-seven-one-three-ten-seven-three-one-five-eight-seven-nine-eight-six-eight-five-ten-five-five-ten-three-two-two-one-six-four-one-six-seven-six-ten-four-eight-three-five-nine-ten-eight-nine-nine-ten-nine-six-two-one-eight-six-ten-eight-two-five-two-four-three-nine-nine-eight-five-three-four-eight-four-five-eight-one-six-two-nine-eight-eight-eight-four-three-eight-three-four-eight-ten-seven-five-three-one-four-three-seven-three-six-seven-one-four-ten-seven-six-four-two-nine-six-four-six-three-one-nine-three-nine-eight-five-six-nine-four-nine-three-four-six-nine-five-nine-three-seven-nine-one-one-ten-five-five-six-one-one-six-one-seven-six-seven-four-seven-two-two-six-one-six-ten-nine-eight-two-one-four-ten-four-three-three-three-three-ten-five-six-six-four-four-four-six-two-two-three-eight-ten-five-ten-ten-two-two-eight-nine-seven-six-three-nine-six-ten-five-one-three-one-six-eight-seven-five-ten-five-three-five-seven-one-three-one-six-five-seven-six-two-one-three-five-four-six-eight-four-nine-nine-eight-eight-three-five-eight-six-five-one-six-five-ten-five-five-four-nine-seven-two-four-six-ten-four-two-ten-ten-six-eight-ten-three-four-five-five-five-five-eight-five-six-six-ten-eight-one-nine-three-four-three-eight-five-seven-six-seven-six-seven-one-one-three-three-three-six-three-three-nine-eight-seven-five-five-ten-six-five-eight-seven-six-six-ten-one-three-four-seven-ten-five-eight-four-eight-seven-four-four-five-four-nine-one-two-ten-two-nine-nine-five-six-two-eight-nine-six-two-two-five-nine-eight-seven-six-five-one-two-one-six-four-nine-seven-eight-eight-nine-four-seven-ten-five-ten-ten-two-nine-seven-one-three-seven-two-seven-five-four-two-six-two-nine-five-nine-five-five-eight-nine-three-four-ten-two-five-ten-three-six-five-nine-two-four-two-nine-five-one-five-six-one-one-nine-seven-five-six-three-seven-nine-five-eight-two-nine-one-ten-ten-eight-seven-three-one-nine-three-one-ten-one-nine-ten-five-one-three-eight-six-two-ten-four-four-seven-five-two-two-ten-ten-five-six-three-ten-four\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. In the cell below, you will find a very long string containing a random sequence of numbers (spelled out) selected from the numbers 2-4. Use string methods to **multiply** all of the numbers up." ] }, { "cell_type": "code", "metadata": {}, "source": [ "numbersString = \"two-three-three-three-two-four-three-three-two-three-two-three-four-four-three-four-three-two-four-four-four-four-two-two-three-three-four-four-two-four-three-three-two-three-three-three-two-two-three-three-four-two-three-four-two-two-two-four-three-four-two-two-three-three-two-four-two-two-three-two-two-three-three-two-four-three-three-two-two-three-four-four-four-four-two-two-four-three-two-three-four-two-four-four-four-three-three-three-two-four-three-three-two-four-three-four-four-four-four-two-three-four-two-three-three-three-four-four-two-two-two-two-four-two-two-three-four-three-two-three-three-two-four-two-four-three-three-four-two-two-three-four-three-three-two-three-four-four-two-four-four-four-three-two-four-two-three-four-four-four-four-two-two-two-four-four-four-two-two-three-three-four-three-two-four-four-four-four-two-two-two-two-three-two-three-two-three-four-three-three-two-three-four-two-three-three-three-two-three-two-four-two-three-three-two-two-two-three-two-two-four-three-four-two-two-three-four-two-four-two-two-three-four-four-four-four-four-two-four-four-three-two-four-two-three-two-three-four-two-two-two-three-four-four-four-two-two-two-four-two-three-three-three-three-three-two-three-two-two-four-three-four-four-three-three-four-three-two-two-four-three-two-four-two-two-four-two-three-two-two-two-two-three-three-two-three-three-four-three-three-three-two-three-four-two-two-three-two-four-two-two-two-three-two-two-three-three-two-two-two-two-two-two-four-two-three-three-four-four-three-two-three-two-two-four-two-three-two-two-three-four-three-four-three-four-four-two-four-three-three-three-two-four-three-four-three-four-three-four-four-two-two-four-three-two-three-three-four-four-four-two-two-two-two-four-four-four-three-four-four-three-two-two-two-two-two-two-three-three-three-three-three-four-three-four-four-three-three-two-three-four-two-three-three-two-three-three-two-four-three-four-two-two-three-two-two-three-four-three-two-four-three-four-two-two-four-two-three-four-two-two-three-three-two-three-three-four-three-three-two-two-two-four-two-three-four-three-two-two-two-four-four-four-two-three-three-two-three-two-two-two-three-four-three-four-three-four-three-two-two-four-four-two-two-four-three-three-two-three-four-two-two-two-three-two-four-two-four-three-two-three-two-three-three-three-four-two-four-four-three-three-two-three-three-four-two-two-four-four-three-three-two-two-four-three-four-three-three-three\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. In the cell below, you will find several variables containing various physical constants. Create f-strings that contain a description of the constant and it's value. For example, for the first variable, you should create a string that looks like: \"Mass of the Earth: 5.98 x 10^24 kg\". Note that you must use the variable given when creating the string; You can't just read the value from the variable and type it in by hand.\n", " 1. Use string methods to replace the \"e\" in the variable with \" x 10^\"\n", " 2. The units for each number is included in the comments. Include this unit in the string. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "massEarth = 5.98e24 # kilograms\n", "radiusEarth = 6.37e6 # meters\n", "couloumbConstant = 8.99e9 # N m^2/C^2\n", "planksConstant = 6.63e-34 # J s\n", "massElectron = 9.11e-31 # kilograms\n", "speedLight = 3.0e8 # m/s\n", "gravitationalConstant = 6.67e-11 # N m^2/kg^2\n", "fundCharge = 1.602e-19 # Coulombs\n", "idealGasConstant = 8.314 # J/(mol Kelvin)\n", "standardAtmosphere = 1.013e5 # Pascals\n", "boltzmannConstant = 1.38e-23 # J/K\n", "massProton = 1.6726e-27 # kg\n" ], "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "name": "python3", "language": "python", "display_name": "Python 3 (ipykernel)" } }, "nbformat": 4, "nbformat_minor": 4 }