{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 不可变的字符串与「可变的」字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "from collections.abc import Sequence\n",
    "\n",
    "s = \"abc\"\n",
    "print(isinstance(s, Sequence))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "ab\n"
     ]
    }
   ],
   "source": [
    "print(s[0])\n",
    "print(s[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# can't mute string.\n",
    "# s = \"abc\"\n",
    "# s[0] = \"b\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1: abc, address: 140669038667824\n",
      "s2: abc, address: 140669042995440\n"
     ]
    }
   ],
   "source": [
    "s1 = \"abc\"\n",
    "s2 = s1.lower()\n",
    "print(f\"s1: {s1}, address: {id(s1)}\")\n",
    "print(f\"s2: {s2}, address: {id(s2)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Zen of Python\n",
      "Beautiful is better than ugly.\n",
      "Explicit is better than implicit.\n",
      "Simple is better than complex.\n",
      "Complex is better than complicated.\n",
      "Flat is better than nested.\n",
      "Sparse is better than dense.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "poetry = \"\"\"The Zen of Python\n",
    "Beautiful is better than ugly.\n",
    "Explicit is better than implicit.\n",
    "Simple is better than complex.\n",
    "Complex is better than complicated.\n",
    "Flat is better than nested.\n",
    "Sparse is better than dense.\n",
    "\"\"\"\n",
    "print(poetry)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 % 号占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 100gle! Today is 2022-05-12, temperature is 17 ℃\n"
     ]
    }
   ],
   "source": [
    "# % placeholder\n",
    "from datetime import datetime\n",
    "\n",
    "name = \"100gle\"\n",
    "now = datetime.now().date()\n",
    "temperature = 17\n",
    "\n",
    "s = \"Hello, %s! Today is %s, temperature is %d ℃\" % (name, now, temperature)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## format 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 100gle! Today is 2022-05-12, temperature is 17 ℃\n",
      "Hello, 100gle! Today is 2022-05-12, temperature is 17 ℃\n",
      "Hello, 100gle! Today is 2022-05-12, temperature is 17 ℃\n"
     ]
    }
   ],
   "source": [
    "# {} placeholder with format method\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "name = \"100gle\"\n",
    "now = datetime.now().date()\n",
    "temperature = 17\n",
    "\n",
    "print(\"Hello, {}! Today is {}, temperature is {} ℃\".format(name, now, temperature))\n",
    "print(\"Hello, {1}! Today is {0}, temperature is {2} ℃\".format(now, name, temperature))\n",
    "print(\"Hello, {name}! Today is {now}, temperature is {temperature} ℃\".format(\n",
    "    name=name, now=now, temperature=temperature)\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## f-string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 100gle! Today is 2022-05-12, temperature is 17 ℃\n"
     ]
    }
   ],
   "source": [
    "# f-string\n",
    "from datetime import datetime\n",
    "\n",
    "\n",
    "print(f\"Hello, {'100gle'}! Today is {datetime.now().date()}, temperature is {17} ℃\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "keep five digits: 3.14159\n",
      "with comma: 1,000,000.00\n",
      "indent left: 100gle    \n",
      "indent right:     100gle\n",
      "indent center:   100gle  \n"
     ]
    }
   ],
   "source": [
    "from math import pi\n",
    "\n",
    "print(f\"keep five digits: {pi:.5f}\")\n",
    "print(f\"with comma: {1000000:,.2f}\")\n",
    "print(f\"indent left: {'100gle':<10}\")\n",
    "print(f\"indent right: {'100gle':>10}\")\n",
    "print(f\"indent center: {'100gle':^10}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用的字符串方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串大小写控制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "str.capitalize: Hello, world\n",
      "str.title: Hello, World\n",
      "str.upper: HELLO, WORLD\n",
      "str.lower: hello, world\n"
     ]
    }
   ],
   "source": [
    "s = \"hello, world\"\n",
    "print(f\"str.capitalize: {s.capitalize()}\")\n",
    "print(f\"str.title: {s.title()}\")\n",
    "print(f\"str.upper: {s.upper()}\")\n",
    "print(f\"str.lower: {s.lower()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串前缀与后缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "001-西游记-小说      is start with 002? False\n",
      "001-西游记-小说      is end with 小说? True\n",
      "001-三国演义-小说     is start with 002? False\n",
      "001-三国演义-小说     is end with 小说? True\n",
      "002-乔布斯传-传记     is start with 002? True\n",
      "002-乔布斯传-传记     is end with 小说? False\n",
      "003-论语-古籍       is start with 002? False\n",
      "003-论语-古籍       is end with 小说? False\n",
      "014-品三国-历史      is start with 002? False\n",
      "014-品三国-历史      is end with 小说? False\n"
     ]
    }
   ],
   "source": [
    "bookshelf = [\n",
    "    \"001-西游记-小说\",\n",
    "    \"001-三国演义-小说\",\n",
    "    \"002-乔布斯传-传记\",\n",
    "    \"003-论语-古籍\",\n",
    "    \"014-品三国-历史\",\n",
    "]\n",
    "\n",
    "for book in bookshelf:\n",
    "    print(f\"{book:<15} is start with 002?\", book.startswith(\"002\"))\n",
    "    print(f\"{book:<15} is end with 小说?\", book.endswith(\"小说\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处理字符串\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "str.replace:  Stay hungry, stay wise \n",
      "str.split: [' Stay hungry', 'stay foolish ']\n",
      "str.strip: Stay hungry, stay foolish\n",
      "str.join: Stay, hungry, stay, foolish\n"
     ]
    }
   ],
   "source": [
    "s = \" Stay hungry, stay foolish \"\n",
    "\n",
    "print(f\"str.replace: {s.replace('foolish', 'wise')}\")\n",
    "print(f\"str.split: {s.split(', ')}\")\n",
    "print(f\"str.strip: {s.strip()}\")\n",
    "print(f\"str.join: {', '.join(['Stay', 'hungry', 'stay', 'foolish'])}\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "13977d4cc82dee5f9d9535ceb495bd0ab12a43c33c664e5f0d53c24cf634b67f"
  },
  "kernelspec": {
   "display_name": "Python 3.9.0 ('pandas-startup')",
   "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.9.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
