{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de0911a3",
   "metadata": {},
   "source": [
    "# A Brief Introduction to Python f-Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46d3d64d",
   "metadata": {},
   "source": [
    "## f-Strings are used in Python to \"pretty print\" (i.e. format) printed results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d20dcd5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-121.41000000000001\n",
      "The product of myVar1 and myVar2 is:  -121.41000000000001\n"
     ]
    }
   ],
   "source": [
    "# Here's a typical Python print statement with very little print formatting:\n",
    "myVar1 = 21.3\n",
    "myVar2 = -5.7\n",
    "myVar3 = myVar1 * myVar2\n",
    "print(myVar3)\n",
    "#    or    \n",
    "print(\"The product of myVar1 and myVar2 is: \", myVar3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "564c0014",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using Numeric variable = 3.141592653589793\n",
      "|        3.141592653589793|\n",
      "|3.141592653589793        |\n",
      "|        3.141592653589793|\n",
      "|    3.141592653589793    |\n",
      "\n",
      "Using String variable = 'Python 3.12.7'\n",
      "|Python 3.12.7            |\n",
      "|Python 3.12.7            |\n",
      "|            Python 3.12.7|\n",
      "|      Python 3.12.7      |\n"
     ]
    }
   ],
   "source": [
    "# f-Strings give us much more capability in formatting and therefore conveying the results of our programs.\n",
    "# For a nice introduction to f-Strings check out:\n",
    "#    http://cissandbox.bentley.edu/sandbox/wp-content/uploads/2022-02-10-Documentation-on-f-strings-Updated.pdf\n",
    "#\n",
    "import math                            # import Python's \"math\" module\n",
    "import sys                             # import Python's \"system\" module\n",
    "variable = math.pi                     # set \"variable\" = the math module very accurate representation of pi.\n",
    "\n",
    "print(f\"Using Numeric {variable = }\")  # The name of the variable you're printing goes in {} (curly braces).\n",
    "                                       # We're using a 25 character wide field.\n",
    "print(f\"|{variable:25}|\")              # Here we use the vertical bar character known as \"pipe\" before and after.\n",
    "print(f\"|{variable:<25}|\")             # Here we left   justify the printed value.\n",
    "print(f\"|{variable:>25}|\")             # Here we right  justify the printed value.\n",
    "print(f\"|{variable:^25}|\\n\")           # Here we center justify the printed value.\n",
    "\n",
    "variable = sys.version                 # Here we do the sames things with a string variable\n",
    "variable = \"Python \" + variable[0:6]\n",
    "print(f\"Using String {variable = }\") \n",
    "print(f\"|{variable:25}|\") \n",
    "print(f\"|{variable:<25}|\") \n",
    "print(f\"|{variable:>25}|\") \n",
    "print(f\"|{variable:^25}|\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f9b8587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using Numeric variable = 3.141592653589793\n",
      "|3.141592653589793========|\n",
      "|========3.141592653589793|\n",
      "|====3.141592653589793====|\n",
      "\n",
      "Using String variable = 'Python 3.12.7'\n",
      "|Python 3.12.7============|\n",
      "|============Python 3.12.7|\n",
      "|======Python 3.12.7======|\n"
     ]
    }
   ],
   "source": [
    "# Here we do the same but we provide a fill character instead of just spaces.\n",
    "# Our variable, variable, is still equal to the current system Python version.\n",
    "variable = math.pi\n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"|{variable:=<25}|\") \n",
    "print(f\"|{variable:=>25}|\") \n",
    "print(f\"|{variable:=^25}|\\n\") \n",
    "\n",
    "variable = sys.version                 # Here we do the sames things with a string variable\n",
    "variable = \"Python \" + variable[0:6]\n",
    "print(f\"Using String {variable = }\") \n",
    "print(f\"|{variable:=<25}|\") \n",
    "print(f\"|{variable:=>25}|\") \n",
    "print(f\"|{variable:=^25}|\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "deee1957",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using Numeric variable = 10\n",
      "This prints without formatting 10\n",
      "This prints with formatting 10\n",
      "This prints also with formatting 10\n",
      "This prints with spacing         10\n",
      "\n",
      "Using Numeric variable = 3.141592653589793\n",
      "This prints without formatting 3.141592653589793\n",
      "This prints with formatting 3.141593\n",
      "This prints with spacing             3.141593\n",
      "\n",
      "Using Numeric variable = 4\n",
      "This prints without formatting 4\n",
      "This prints with percent formatting 400.000000%\n",
      "\n",
      "Using Numeric variable = 403267890\n",
      "This prints with exponential formatting 4.032679e+08\n"
     ]
    }
   ],
   "source": [
    "# Here is a summary of how to format various data types using f-Strings:\n",
    "# Type  Meaning\n",
    "# ----  ------------------------------------------------------------------------------------------------------------\n",
    "#   s   String format—this is the default type for strings\n",
    "#   d   Decimal Integer. This uses a comma as the number separator character. \n",
    "#   n   Number. This is the same as d except that it uses the current locale setting to \n",
    "#       insert the appropriate number separator characters. \n",
    "#   e   Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. \n",
    "#       The default precision is 6. \n",
    "#   f   Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6.\n",
    "#   %   Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. \n",
    "\n",
    "variable = 10 \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"This prints without formatting {variable}\") \n",
    "print(f\"This prints with formatting {variable:d}\") \n",
    "print(f\"This prints also with formatting {variable:n}\") \n",
    "print(f\"This prints with spacing {variable:10d}\\n\") \n",
    "\n",
    "variable = math.pi \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"This prints without formatting {variable}\") \n",
    "print(f\"This prints with formatting {variable:f}\") \n",
    "print(f\"This prints with spacing {variable:20f}\\n\") \n",
    "\n",
    "variable = 4 \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"This prints without formatting {variable}\") \n",
    "print(f\"This prints with percent formatting {variable:%}\\n\") \n",
    "\n",
    "variable = 403267890 \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"This prints with exponential formatting {variable:e}\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2a3efefd",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using Numeric variable = 1200356.8796\n",
      "With two decimal places: 1200356.88\n",
      "With four decimal places: 1200356.8796\n",
      "With two decimal places and a comma: 1,200,356.88\n",
      "With a forced plus sign: +1200356.88\n",
      "\n",
      "Using Numeric variable = -1200356.8796\n",
      "With two decimal places and a comma: -1,200,356.88\n"
     ]
    }
   ],
   "source": [
    "variable = 1200356.8796 \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"With two decimal places: {variable:.2f}\") \n",
    "print(f\"With four decimal places: {variable:.4f}\") \n",
    "print(f\"With two decimal places and a comma: {variable:,.2f}\") \n",
    "print(f\"With a forced plus sign: {variable:+.2f}\\n\") \n",
    "\n",
    "variable *= -1 \n",
    "print(f\"Using Numeric {variable = }\") \n",
    "print(f\"With two decimal places and a comma: {variable:,.4f}\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "585ae52d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number   Square       Cube\n",
      " 1         1             1\n",
      " 2         4             8\n",
      " 3         9            27\n",
      " 4        16            64\n",
      " 5        25           125\n",
      " 6        36           216\n",
      " 7        49           343\n",
      " 8        64           512\n",
      " 9        81           729\n",
      "10       100          1000\n"
     ]
    }
   ],
   "source": [
    "# Use of Tabs and Spacing Program \n",
    "# output is often required to be in tabular form. f-strings are very useful in formatting this kind of output. \n",
    "# The following lines produce a tidily aligned set of columns with integers and their squares and cubes, \n",
    "# using spaces to align the columns. \n",
    "# Notice the use of the field width to ensure that all the numbers are right-aligned: \n",
    "print(f'Number   Square       Cube') \n",
    "for x in range(1, 11): \n",
    "    print(f'{x:2d}       {x*x:3d}          {x*x*x:4d}') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28b43c9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# The tab character (\\t) can also be used in an f-string to line up columns, \n",
    "# particularly when column headings are used: \n",
    "print(f'Number\\t\\t\\tSquare\\t\\t\\tCube') \n",
    "for x in range(1, 11):\n",
    "    print(f'{x:2d}\\t\\t\\t{x*x:3d}\\t\\t\\t{x*x*x:4d}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de6c45f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using either tabs or spacing is acceptable and may depend on which one you are more comfortable with. \n",
    "# Either way is acceptable in Python. \n",
    "# The following takes the previous program and converts x to a float() so that formatting of floating-point numbers \n",
    "# can be demonstrated: \n",
    "print(f'Number\\t\\tSquare\\t\\tCube') \n",
    "for x in range(1, 11):\n",
    "    x = float(x)\n",
    "    print(f'{x:5.2f}\\t\\t{x*x:6.2f}\\t\\t{x*x*x:8.2f}') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47fcbb2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This also demonstrates how the use of a value for width will enable the columns to line up. \n",
    "# The following program demonstrates the use of strings, decimals, and floats, as well as tabs \n",
    "# for a type of report that is often produced in a typical Python program. \n",
    "# Notice the use of the dollar sign ($) just before the variables that are to be displayed as prices. \n",
    "APPLES = .50 \n",
    "BREAD = 1.50 \n",
    "CHEESE = 2.25 \n",
    "\n",
    "numApples = 3\n",
    "numBread = 10 \n",
    "numCheese = 6 \n",
    "\n",
    "prcApples = numApples * APPLES \n",
    "prcBread = numBread* BREAD \n",
    "prcCheese = numCheese * CHEESE \n",
    "\n",
    "strApples = 'Apples' \n",
    "strBread = 'Rye Bread' \n",
    "strCheese = 'Cheese' \n",
    "\n",
    "total = prcBread + prcCheese + prcApples \n",
    "\n",
    "print(f'{\"My Grocery List\":^30s}') \n",
    "print(f'{\"=\"*30}') \n",
    "print(f'{strApples:10s}{numApples:10d}\\t${prcApples:>5.2f}') \n",
    "print(f'{strBread:10s}{numBread:10d}\\t${prcBread:>5.2f}') \n",
    "print(f'{strCheese:10s}{numCheese:10d}\\t${prcCheese:>5.2f}') \n",
    "print(f'{\"Total:\":>20s}\\t${total:>5.2f}') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c85047aa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
