{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "835c0920-8b71-494a-bf6d-116a53c6bcf5",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2024<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa6e822c-4404-40cf-b972-910ece9e64e4",
   "metadata": {},
   "source": [
    "# 3.10 Formatted Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "febb0d65-199e-4083-a331-7eb0954bac14",
   "metadata": {},
   "source": [
    "#### Use an **f-string** to format a string with embedded values.\n",
    "#### Inside of an f-string, enclose values to embed in curly braces, such as `{a}`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dda43477-f151-4569-a928-c1fbe9ec617b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adbeb39d-1dda-4801-b255-e45e5e6018ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = 5\n",
    "sum = a + b\n",
    "\n",
    "str = f'The sum of {a}, {b}, and {c} is {sum}'\n",
    "str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73878ed8-8339-4969-b330-0c7c30e1ff56",
   "metadata": {},
   "source": [
    "#### We can use an **f-string** to format our printed output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "265be183-e88a-4f26-88c2-1a3d3b42e73b",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'The sum of {a}, {b}, and {c} is {sum}')\n",
    "print(f'and their average is {sum/3}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d817ddc5-26a4-4e60-b411-78ec181b1ade",
   "metadata": {},
   "source": [
    "#### You can specify how many spaces you want a printed value to take up. However, if a number needs more space, it will take up as many as it needs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7b2a8a6-6f68-4f6c-acc5-65a9223c9f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'The sum of {a:5}, {b:5}, and {c:5} is {sum:10}')\n",
    "print(f'and their average is {sum/3:4}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "847d59da-e3c6-4bb0-9993-18870d2d067c",
   "metadata": {},
   "source": [
    "## The d and f format specifiers."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cef2b81-91b1-4e90-9074-02b9b1b31e4f",
   "metadata": {},
   "source": [
    "#### You can end each format specification for an integer value with a `d`. For floating-point numbers such as the average above, you can specify both how many spaces and how many places after the decimal point. End each format specification for a floating-point value with an `f` if you specify the number of decimal places. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98a9bf85-0f0d-49d9-8633-44bfa719646a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'The sum of {a:5}, {b:5}, and {c:5d} is {sum:10d}')\n",
    "print(f'and their average is {sum/3:4.2f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17cfc062-c56b-4b3e-b5eb-52da904df05c",
   "metadata": {},
   "source": [
    "#### When specifying the number of spaces for a value, be sure to include any leading minus sign and, for floating-point values, a space for the decimal point.\n",
    "- The printed value will be rounded to the number of decimal places you specify. \n",
    "- If you specify 0 decimal places, the number will print like an integer value, rounded if necessary. \n",
    "- You can also only specify the number of decimal places, and the printed value will take only as many spaces as it needs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8287e660-1912-43f5-8abb-5133d0871246",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = -4.6387\n",
    "\n",
    "print(f'The value of alpha is {alpha:8.3f}')\n",
    "print(f'The value of alpha is {alpha:8.0f}')\n",
    "print(f'The value of alpha is {alpha:.2f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "726a8136-23b9-407f-8703-bc4901ce115e",
   "metadata": {},
   "source": [
    "## Automatic labeling with '='\n",
    "#### This is a great feature to print debugging output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3887b0c-9e4c-485b-a7ba-c4a160e76809",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 12345\n",
    "b = 1_234_567_890\n",
    "c = 123456.987665\n",
    "\n",
    "print(f'{a = :6d}')\n",
    "print(f'{b = :11d}')\n",
    "print(f'{c = :12.4f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93204514-02e7-4b89-bfc6-d2630da883a3",
   "metadata": {},
   "source": [
    "## Commas in printed numeric values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47b8dd60-ad11-4fb9-95a3-3e94379d19a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 12345\n",
    "\n",
    "print(f'{a:6,d}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6795773-4925-44a1-80bf-d942fe85b183",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 12345\n",
    "b = 1_234_567_890\n",
    "c = 123456.987654\n",
    "\n",
    "print(f'{a = :6,d}')\n",
    "print(f'{b = :7,d}')\n",
    "print(f'{c = :12,.4f}')  ## note where the comma goes!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6274619a-472c-4030-96ad-7ba14a6a3eb2",
   "metadata": {},
   "source": [
    "## Left- and right-justifying and centering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eeda0274-bb47-440c-adbc-12d25bc26e6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'Right-justification is default: |{alpha:10.3f}|')\n",
    "print(f'Specify right-justification:    |{alpha:>10.3f}|')\n",
    "print(f'Specify  left-justification:    |{alpha:<10.3f}|')\n",
    "print(f'Specify centering:              |{alpha:^10.3f}|')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3ef455d-485d-4ccc-b85a-08598aef12a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (C) Copyright 2024 by Ronald Mak"
   ]
  }
 ],
 "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
