{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f261f380",
   "metadata": {},
   "source": [
    "# 08-06 · Форматирование строк\n",
    "\n",
    "Практика к разделу [«Форматирование строк»](../../site/chapters/glava-08/08-06-formatirovanie-strok.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01f87784",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Сравнить %, .format() и f-строки на практике."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f169cc9",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc6f761d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:48.982269Z",
     "iopub.status.busy": "2026-08-14T20:53:48.982114Z",
     "iopub.status.idle": "2026-08-14T20:53:49.001556Z",
     "shell.execute_reply": "2026-08-14T20:53:49.001120Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Привет, Cartesian! Тебе 5 лет.\n",
      "Привет, Cartesian! Тебе 5 лет.\n",
      "Привет, Cartesian! Тебе 5 лет.\n"
     ]
    }
   ],
   "source": [
    "name = \"Cartesian\"\n",
    "age = 5\n",
    "\n",
    "print(\"Привет, %s! Тебе %d лет.\" % (name, age))\n",
    "print(\"Привет, {}! Тебе {} лет.\".format(name, age))\n",
    "print(f\"Привет, {name}! Тебе {age} лет.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce3be372",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — выражения внутри f-строки"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "acbebd17",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.002573Z",
     "iopub.status.busy": "2026-08-14T20:53:49.002461Z",
     "iopub.status.idle": "2026-08-14T20:53:49.004761Z",
     "shell.execute_reply": "2026-08-14T20:53:49.004317Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Через год будет 6.\n",
      "Заглавными: CARTESIAN\n"
     ]
    }
   ],
   "source": [
    "age = 5\n",
    "print(f\"Через год будет {age + 1}.\")\n",
    "print(f\"Заглавными: {name.upper()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d9b571c",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — форматирование чисел"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0d30c3d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.005653Z",
     "iopub.status.busy": "2026-08-14T20:53:49.005549Z",
     "iopub.status.idle": "2026-08-14T20:53:49.007838Z",
     "shell.execute_reply": "2026-08-14T20:53:49.007402Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Пи округлённо: 3.14\n",
      "1,234,567\n"
     ]
    }
   ],
   "source": [
    "pi = 3.14159265\n",
    "print(f\"Пи округлённо: {pi:.2f}\")\n",
    "print(f\"{1234567:,}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb4faf9f",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Выведите таблицу «имя — возраст» тремя способами (%, .format(), f-строка) для одних и тех же данных, сравните длину кода."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9d99cb73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.009119Z",
     "iopub.status.busy": "2026-08-14T20:53:49.009005Z",
     "iopub.status.idle": "2026-08-14T20:53:49.011472Z",
     "shell.execute_reply": "2026-08-14T20:53:49.011086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ада — 28\n",
      "Ада — 28\n",
      "Ада — 28\n"
     ]
    }
   ],
   "source": [
    "name, age = \"Ада\", 28\n",
    "print(\"%s — %d\" % (name, age))\n",
    "print(\"{} — {}\".format(name, age))\n",
    "print(f\"{name} — {age}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "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.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
