{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ad1d0ce",
   "metadata": {},
   "source": [
    "# 15-16 · Текст, bytes и кодировка UTF-8\n",
    "\n",
    "Практика к разделу [«Текст, bytes и UTF-8»](../../site/chapters/glava-15/15-16-text-bytes-encoding.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80d7a35c",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "81d56c29",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:00.188356Z",
     "iopub.status.busy": "2026-08-18T15:53:00.188259Z",
     "iopub.status.idle": "2026-08-18T15:53:00.204321Z",
     "shell.execute_reply": "2026-08-18T15:53:00.203833Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 10 Питон\n"
     ]
    }
   ],
   "source": [
    "text = \"Питон\"\n",
    "encoded = text.encode(\"utf-8\")\n",
    "chars_len = len(text)\n",
    "bytes_len = len(encoded)\n",
    "decoded_back = encoded.decode(\"utf-8\")\n",
    "print(chars_len, bytes_len, decoded_back)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1168cbc8",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Запишите строку с кириллицей в файл с явным encoding=\"utf-8\" и прочитайте её обратно."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "33c9080f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:00.205268Z",
     "iopub.status.busy": "2026-08-18T15:53:00.205148Z",
     "iopub.status.idle": "2026-08-18T15:53:00.208063Z",
     "shell.execute_reply": "2026-08-18T15:53:00.207458Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Привет!\n"
     ]
    }
   ],
   "source": [
    "with open(\"privet3.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"Привет!\")\n",
    "\n",
    "with open(\"privet3.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    read_back = f.read()\n",
    "print(read_back)"
   ]
  }
 ],
 "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
}
