{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ed9825f2-6751-497b-9c6d-899fffc4c004",
   "metadata": {},
   "source": [
    "# Stringy - vybrané operace a metody"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a1b3a8dc-3bc4-430b-b97d-ed3092bd6fad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "je tam\n",
      "a h o j   l i d i 9\n",
      "i\n",
      "lid\n",
      "bca\n",
      "bcabca\n",
      "bcabcabcabcabcabca\n"
     ]
    }
   ],
   "source": [
    "# opeace pro posloupnosti:\n",
    "s = \"ahoj lidi\"\n",
    "\n",
    "# in, not in \n",
    "if 'a' in s:\n",
    "    print(\"je tam\")\n",
    "\n",
    "# for cyklus\n",
    "for z in s:\n",
    "    print(z, end = \" \")\n",
    "\n",
    "# počet znaků (len)\n",
    "print(len(s))\n",
    "\n",
    "# indexace\n",
    "print(s[8])\n",
    "\n",
    "# výřez\n",
    "print(s[5:8])\n",
    "\n",
    "# operátory +, * (zřetězení)\n",
    "s = \"\"\n",
    "s += \"a\"\n",
    "s = \"bc\" + s\n",
    "print(s)\n",
    "\n",
    "s *= 2\n",
    "print(s)\n",
    "print(3 * s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4804abb2-ebe8-448b-8563-d69255b95d27",
   "metadata": {},
   "source": [
    " ## Základní metody\n",
    "- spojování a rozdělování (např. `join`, `split`, `splitlines`, `partition`, `rpartition`)\n",
    "- zjišťovací metody (např. `islower`, `isupper`, `isalpha`, `isspace`, `isdigit`, `isalnum`,...)\n",
    "- vyhledávání podstringů (např. `count`, `find`, `rfind` `index`, `rindex`, `startswith`, `endswith`)\n",
    "- nahrazování (např. `replace`, `expandtabs`)\n",
    "- ořezávání (např. `strip`, `rstrip`, `lstrip`)\n",
    "- formátování (např. `lower`, `upper`, `capitalize`, `title`, `ljust`, `rjust`, `center`)\n",
    "\n",
    "Detaily o všech metodách třídy `str` nalzenete v dokumentaci https://docs.python.org/3/library/stdtypes.html#string-methods."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9999759-76d8-4806-93e9-ff6425c7b682",
   "metadata": {},
   "source": [
    "Spojování a rozdělování (např. `join`, `split`, `splitlines`, `partition`, `rpartition`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a77612ec-c3e7-4b0d-9fed-ab87a3035da6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a sentence.\n",
      "['This', 'is', 'a', 'sentence.', 'This', 'is', 'another', 'sentence.']\n",
      "['bread', 'butter', 'sousage']\n",
      "('bread', ' and ', 'butter and sousage')\n",
      "('bread and butter', ' and ', 'sousage')\n"
     ]
    }
   ],
   "source": [
    "print(\" \".join([\"This\", \"is\", \"a\", \"sentence.\"]))                  # spojení stringů\n",
    "print(\"This is a sentence.\\n This is another sentence.\".split())   # rozdělení stringů\n",
    "print(\"bread and butter and sousage\".split(\" and \"))\n",
    "print(\"bread and butter and sousage\".partition(\" and \"))\n",
    "print(\"bread and butter and sousage\".rpartition(\" and \"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fcf2d3d-9c1b-405a-8e2b-558d467486d4",
   "metadata": {},
   "source": [
    "Zjišťovací metody (např. `islower`, `isupper`, `isalpha`, `isspace`, `isdigit`, `isalnum`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "08873556-96f7-4d7e-8ead-83947cfc664a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False True\n"
     ]
    }
   ],
   "source": [
    "print(\"17.5\".isnumeric(), \"ahoj lidi\".islower())                   # zjišťovací metody"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6338b258-4dc6-4b6f-a699-f9ce96f33648",
   "metadata": {},
   "source": [
    "Vyhledávání podstringů (např. `count`, `find`, `rfind` `index`, `rindex`, `startswith`, `endswith`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec34e139-4530-4689-a72b-394c35a60aa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "8\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(\"To není to, co to není, je to toto!\".count(\"to\"))           # počet výskytů podstringu\n",
    "print(\"To není to, co to není, je to toto!\".find(\"to\"))            # index prvního výskytu podstringu\n",
    "print(\"To není to, co to není, je to toto!\".startswith(\"to\", 8))  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa55fc29-04f6-47c4-b615-cbad8353a8af",
   "metadata": {},
   "source": [
    "Nahrazování (např. `replace`, `expandtabs`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aea2f3ca-87ed-4d54-a939-e2b5284af750",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To JE to, co to JE, je to toto!\n"
     ]
    }
   ],
   "source": [
    "print(\"To není to, co to není, je to toto!\".replace(\"není\", \"JE\")) # nahrazení všech výskytů podstringu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b5b73c3-bc72-4e11-92bc-74cbfea2a225",
   "metadata": {},
   "source": [
    "Ořezávání (např. `strip`, `rstrip`, `lstrip`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "859a215a-98e1-45e7-a2c5-8d479b370094",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adam Novák\n",
      "Adam Novák------\n"
     ]
    }
   ],
   "source": [
    "print(\"   Adam Novák \\t\\n \\n\".strip())                             # ořezání stringu\n",
    "print(\"-*-*-*-*-Adam Novák------\".lstrip(\"*-\"))      "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f31ec4c-34e3-42c6-b509-96fdfac07257",
   "metadata": {},
   "source": [
    "Formátování (např. `lower`, `upper`, `capitalize`, `title`, `ljust`, `rjust`, `center`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2b7d1840-eb9c-4999-8396-8dd50706fedc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TO NENÍ TO, CO TO NENÍ, JE TO TOTO!\n",
      "------ Nadpis ------\n"
     ]
    }
   ],
   "source": [
    "print(\"To není to, co to není, je to toto!\".upper())               # jednoduché formátování\n",
    "print(\" Nadpis \".center(20, '-'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1361710-b63e-4ad3-92e9-d524f21e2997",
   "metadata": {},
   "source": [
    "Formátování řetězců"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d0f99ed4-ca9a-4b16-9983-6e8ecf6f1ec8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Výsledek je 1.6666666666666667.\n",
      "Výsledek je 1.67.\n",
      "Ahoj, jsem Matěj. A moje oblibené číslo je 7.\n",
      "Ahoj, jsem Matěj. A moje oblibené číslo je 7.\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "print(f\"Výsledek je {x / 3}.\")\n",
    "print(f\"Výsledek je {x / 3:.2f}.\")\n",
    "\n",
    "s1 = \"Ahoj, jsem {}. A moje oblibené číslo je {}.\"\n",
    "print(s1.format(\"Matěj\", 7))\n",
    "\n",
    "s2 = \"Ahoj, jsem {jmeno}. A moje oblibené číslo je {cislo}.\"\n",
    "print(s2.format(cislo=7, jmeno=\"Matěj\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ca0aa0f-297d-4ad7-84fb-e1d2f107d613",
   "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.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
