diff --git a/format_strings.ipynb b/format_strings.ipynb new file mode 100644 index 0000000..26688c5 --- /dev/null +++ b/format_strings.ipynb @@ -0,0 +1,248 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c23452f9-f689-420e-b170-ee9b8b08e1d0", + "metadata": {}, + "source": [ + "# F String" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d453e396-0588-4982-b254-75add9a0cc08", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The price is 73.75 dollars\n" + ] + } + ], + "source": [ + "price = 59\n", + "tax = 0.25\n", + "txt = f\"The price is {price + (price * tax)} dollars\"\n", + "print(txt)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "273f9c04-aeb7-48d5-ba49-06c24f5a7651", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It is very Cheap\n" + ] + } + ], + "source": [ + "price = 49\n", + "txt = f\"It is very {'Expensive' if price>50 else 'Cheap'}\"\n", + "print(txt)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fbe7e945-6ac1-4c71-8bd9-beb8f425d897", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I love APPLES\n" + ] + } + ], + "source": [ + "fruit = \"apples\"\n", + "txt = f\"I love {fruit.upper()}\"\n", + "print(txt)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "478b71c4-9b59-4cda-8b3e-3c1abccb731a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The plane is flying at a 9144.0 meter altitude\n" + ] + } + ], + "source": [ + "def myconverter(x):\n", + " return x * 0.3048\n", + "\n", + "\n", + "txt = f\"The plane is flying at a {myconverter(30000)} meter altitude\"\n", + "print(txt)" + ] + }, + { + "cell_type": "markdown", + "id": "c053f887-73b5-4838-b42c-63e9d398c9b4", + "metadata": {}, + "source": [ + "# String format()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "becf51c2-f500-4d7f-b388-8bf17835e0eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The price is 49 dollars\n" + ] + } + ], + "source": [ + "price = 49\n", + "txt = \"The price is {} dollars\"\n", + "print(txt.format(price))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e149e803-611a-4334-8b84-56463eebedb9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The price is 49.00 dollars\n" + ] + } + ], + "source": [ + "txt = \"The price is {:.2f} dollars\"\n", + "print(txt.format(price))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "dd616200-4c97-4654-80f1-d09efdc7c43e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I want 3 pieces of item number 567 for 49.00 dollars.\n" + ] + } + ], + "source": [ + "quantity = 3\n", + "itemno = 567\n", + "price = 49\n", + "myorder = \"I want {} pieces of item number {} for {:.2f} dollars.\"\n", + "print(myorder.format(quantity, itemno, price))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "aea41442-f091-4ff8-bde5-1001ec01061f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I want 3 pieces of item number 567 for 49.00 dollars.\n" + ] + } + ], + "source": [ + "quantity = 3\n", + "itemno = 567\n", + "price = 49\n", + "myorder = \"I want {0} pieces of item number {1} for {2:.2f} dollars.\"\n", + "print(myorder.format(quantity, itemno, price))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "b0a150cb-5420-4314-b21b-2e35a25dbb2d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "His name is John. John is 36 years old.\n" + ] + } + ], + "source": [ + "age = 36\n", + "name = \"John\"\n", + "txt = \"His name is {1}. {1} is {0} years old.\"\n", + "print(txt.format(age, name))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "2481de45-9a9d-4e59-abac-65912698b6d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I have a Ford, it is a Mustang.\n" + ] + } + ], + "source": [ + "myorder = \"I have a {carname}, it is a {model}.\"\n", + "print(myorder.format(carname = \"Ford\", model = \"Mustang\"))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}