{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dc13d7de",
   "metadata": {},
   "source": [
    "# 08-14 · Срезы строки\n",
    "\n",
    "Практика к разделам [«Срезы строки»](../../site/chapters/glava-08/08-14-srezy-stroki.html) и [«Строки нельзя изменить»](../../site/chapters/glava-08/08-15-neizmenyaemost.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78db1bdf",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить срезы [start:stop:step] и убедиться, что строки неизменяемы."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e25fd937",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "679e8c1d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T16:18:55.227124Z",
     "iopub.status.busy": "2026-08-16T16:18:55.226980Z",
     "iopub.status.idle": "2026-08-16T16:18:55.253530Z",
     "shell.execute_reply": "2026-08-16T16:18:55.253111Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Car\n",
      "ian\n",
      "naisetraC\n"
     ]
    }
   ],
   "source": [
    "word = \"Cartesian\"\n",
    "print(word[0:3])\n",
    "print(word[-3:])\n",
    "print(word[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a928333",
   "metadata": {},
   "source": [
    "## Типичная ошибка — строку нельзя изменить «на месте»"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "198fd9c7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T16:18:55.254733Z",
     "iopub.status.busy": "2026-08-16T16:18:55.254619Z",
     "iopub.status.idle": "2026-08-16T16:18:55.290442Z",
     "shell.execute_reply": "2026-08-16T16:18:55.289933Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m word = \u001b[33m\"Cat\"\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m word[\u001b[32m0\u001b[39m] = \u001b[33m\"B\"\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "word = \"Cat\"\n",
    "word[0] = \"B\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ff7d3b5",
   "metadata": {},
   "source": [
    "## Исправление"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54670fae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T16:18:55.291765Z",
     "iopub.status.busy": "2026-08-16T16:18:55.291648Z",
     "iopub.status.idle": "2026-08-16T16:18:55.294010Z",
     "shell.execute_reply": "2026-08-16T16:18:55.293542Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bat\n"
     ]
    }
   ],
   "source": [
    "word = \"Cat\"\n",
    "word = \"B\" + word[1:]\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecbe5018",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Для слова «Cartesian» получите срезами: первые три символа `first3`, последние три `last3`, каждый второй символ `every_second` и слово задом наперёд `reversed_word`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f52d269",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-16T16:18:55.295312Z",
     "iopub.status.busy": "2026-08-16T16:18:55.295186Z",
     "iopub.status.idle": "2026-08-16T16:18:55.297990Z",
     "shell.execute_reply": "2026-08-16T16:18:55.297452Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Car ian Crein naisetraC\n"
     ]
    }
   ],
   "source": [
    "word = \"Cartesian\"\n",
    "first3 = word[:3]\n",
    "last3 = word[-3:]\n",
    "every_second = word[::2]\n",
    "reversed_word = word[::-1]\n",
    "print(first3, last3, every_second, reversed_word)"
   ]
  }
 ],
 "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
}
