{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ " # 聪明办法学 Python 2nd Edition\n", "## Chapter 6 字符串 Strings\n", "---\n", "聪明办法学 Python 教学团队\n", "\n", "
learn.python.the.smart.way@gmail.com
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 字符串文字" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 四种引号\n", "\n", "引号的作用就是将文字包裹起来,告诉 Python \"这是个字符串!\"\n", "\n", "单引号 `'` 和双引号 `\"` 是最常见的两种字符串引号" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:08:54.043737Z", "start_time": "2023-09-14T09:08:54.027673Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "单引号\n", "双引号\n" ] } ], "source": [ "print('单引号')\n", "print(\"双引号\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "三个引号的情况不太常见,但是它在一些场合有特定的作用(如函数文档 doc-strings)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:08:55.742102Z", "start_time": "2023-09-14T09:08:55.735056Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "三个单引号\n", "三个双引号\n" ] } ], "source": [ "print('''三个单引号''')\n", "print(\"\"\"三个双引号\"\"\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**我们为什么需要两种不同的引号?**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:08:57.780088Z", "start_time": "2023-09-14T09:08:57.773238Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "聪明办法学 Python 第二版的课程简称是 'P2S'\n" ] } ], "source": [ "# 为了写出这样的句子\n", "print(\"聪明办法学 Python 第二版的课程简称是 'P2S'\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "如果我们偏要只用一种引号呢?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:08:59.917890Z", "start_time": "2023-09-14T09:08:59.903383Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (3461323572.py, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn [4], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m print(\"聪明办法学 Python 第二版的课程简称是 \"P2S\"\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "# 这会导致语法错误,Python 无法正确判断一个字符串的终止位置\n", "print(\"聪明办法学 Python 第二版的课程简称是 \"P2S\"\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 字符串中的换行符号\n", "\n", "前面有反斜杠 `\\` 的字符,叫做**转义序列**\n", "\n", "比如 `\\n` 代表**换行**,尽管它看起来像两个字符,但是 Python 依然把它视为一个特殊的字符" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:02.141492Z", "start_time": "2023-09-14T09:09:02.124774Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data\n", "whale\n" ] } ], "source": [ "# 这两个 print() 在做同样的事情 \n", "print(\"Data\\nwhale\") # \\n 是一个单独的换行符号" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:03.257491Z", "start_time": "2023-09-14T09:09:03.249970Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data\n", "whale\n" ] } ], "source": [ "print(\"\"\"Data\n", "whale\"\"\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:04.590053Z", "start_time": "2023-09-14T09:09:04.580955Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "你可以在字符串后面使用 反斜杠 `\\` 来排除后面的换行。比如这里是第二行文字,但是你会看到它会紧跟在上一行句号后面。这种做法在 CIL 里面经常使用(多个 Flag 并排保持美观),但是在编程中的应用比较少。\n" ] } ], "source": [ "print(\"\"\"你可以在字符串后面使用 反斜杠 `\\` 来排除后面的换行。\\\n", "比如这里是第二行文字,但是你会看到它会紧跟在上一行句号后面。\\\n", "这种做法在 CIL 里面经常使用(多个 Flag 并排保持美观),\\\n", "但是在编程中的应用比较少。\\\n", "\"\"\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:06.504664Z", "start_time": "2023-09-14T09:09:06.497632Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "你可以在字符串后面使用 反斜杠 `\\` 来排除后面的换行。\n", "比如这里是第二行文字,但是你会看到它会紧跟在上一行句号后面。\n", "这种做法在 CIL 里面经常使用(多个 Flag 并排保持美观),\n", "但是在编程中的应用比较少。\n", "\n" ] } ], "source": [ "print(\"\"\"你可以在字符串后面使用 反斜杠 `\\` 来排除后面的换行。\n", "比如这里是第二行文字,但是你会看到它会紧跟在上一行句号后面。\n", "这种做法在 CIL 里面经常使用(多个 Flag 并排保持美观),\n", "但是在编程中的应用比较少。\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 其他的转义序列" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:10.235167Z", "start_time": "2023-09-14T09:09:10.221255Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "双引号:\"\n" ] } ], "source": [ "print(\"双引号:\\\"\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:11.334012Z", "start_time": "2023-09-14T09:09:11.319471Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "反斜线:\\\n" ] } ], "source": [ "print(\"反斜线:\\\\\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:12.318311Z", "start_time": "2023-09-14T09:09:12.301296Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "换\n", "行\n" ] } ], "source": [ "print(\"换\\n行\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:13.284332Z", "start_time": "2023-09-14T09:09:13.273784Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "这个是\t制\t表\t符\n", "也叫\t跳\t格\t键\n" ] } ], "source": [ "print(\"这个是\\t制\\t表\\t符\\n也叫\\t跳\\t格\\t键\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "转义序列只作为一个字符存在" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:15.670090Z", "start_time": "2023-09-14T09:09:15.660067Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s = D\\a\"t\ta\n", "\n", "s 的长度为: 7\n" ] } ], "source": [ "s = \"D\\\\a\\\"t\\ta\"\n", "print(\"s =\", s)\n", "print(\"\\ns 的长度为:\", len(s))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## repr() vs. print()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "我们现在有两个字符串" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:18.851095Z", "start_time": "2023-09-14T09:09:18.845148Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s1 = \"Data\\tWhale\"\n", "s2 = \"Data Whale\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "它俩看起来似乎是一样的" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:20.879194Z", "start_time": "2023-09-14T09:09:20.869076Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s1: Data\tWhale\n", "s2: Data Whale\n" ] } ], "source": [ "print(\"s1:\", s1)\n", "print(\"s2:\", s2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "但是它们真的一样吗?" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:22.771850Z", "start_time": "2023-09-14T09:09:22.755795Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 == s2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ " > ### 如来佛合掌道:“观音尊者,你看那两个行者,谁是真假?” " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "> ### “谛听,汝之神通,能分辨出谁是真身,可为我说之。”" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:25.690072Z", "start_time": "2023-09-14T09:09:25.685560Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Data\\tWhale'\n", "'Data Whale'\n" ] } ], "source": [ "print(repr(s1))\n", "print(repr(s2))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:27.127485Z", "start_time": "2023-09-14T09:09:27.124484Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "hack_text = \"密码应当大于 8 个字符,小于 16 个字符,包含大写字母、小写字母、数字和特殊符号\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:28.373296Z", "start_time": "2023-09-14T09:09:28.364279Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "密码应当大于 8 个字符,小于 16 个字符,包含大写字母、小写字母、数字和特殊符号\t\t\t\t\t\t\t\t\t\t\t\t\t\n" ] } ], "source": [ "print(hack_text)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:29.463962Z", "start_time": "2023-09-14T09:09:29.451923Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'密码应当大于 8 个字符,小于 16 个字符,包含大写字母、小写字母、数字和特殊符号\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t'\n" ] } ], "source": [ "print(repr(hack_text))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "多行字符串作为注释\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:30.718225Z", "start_time": "2023-09-14T09:09:30.706686Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Amazing!\n" ] } ], "source": [ "\"\"\"\n", "Python 本身是没有多行注释的,\n", "但是你可以用多行字符串实现同样的操作,\n", "还记得我们之前学过的“表达式“吗?\n", "它的原理就是 Python 会运行它,\n", "但是马上扔掉!(垃圾回收机制)\n", "\"\"\"\n", "print(\"Amazing!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# 一些字符串常量" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:33.967226Z", "start_time": "2023-09-14T09:09:33.949010Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n" ] } ], "source": [ "import string\n", "print(string.ascii_letters)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:34.956185Z", "start_time": "2023-09-14T09:09:34.945054Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdefghijklmnopqrstuvwxyz\n" ] } ], "source": [ "print(string.ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:35.893654Z", "start_time": "2023-09-14T09:09:35.881602Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" ] } ], "source": [ "print(string.ascii_uppercase) " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:36.997888Z", "start_time": "2023-09-14T09:09:36.984320Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0123456789\n" ] } ], "source": [ "print(string.digits)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:38.059200Z", "start_time": "2023-09-14T09:09:38.049236Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n" ] } ], "source": [ "print(string.punctuation) # < = >" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:38.994218Z", "start_time": "2023-09-14T09:09:38.977242Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n", "\r\n", "\u000b\n", "\n", "\f\n", "\n", "\n" ] } ], "source": [ "print(string.printable)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:40.023145Z", "start_time": "2023-09-14T09:09:40.007358Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \t\n", "\r\n", "\u000b\n", "\n", "\f\n", "\n", "\n" ] } ], "source": [ "print(string.whitespace)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:43.249150Z", "start_time": "2023-09-14T09:09:43.235124Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "' \\t\\n\\r\\x0b\\x0c'\n" ] } ], "source": [ "print(repr(string.whitespace))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# 一些字符串的运算\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "字符串的加减" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:45.521869Z", "start_time": "2023-09-14T09:09:45.506240Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdef\n", "abcabcabc\n" ] } ], "source": [ "print(\"abc\" + \"def\")\n", "print(\"abc\" * 3)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:47.408043Z", "start_time": "2023-09-14T09:09:47.050339Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [31], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mabc\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m)\n", "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "print(\"abc\" + 3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`in` 运算(超级好用!)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:49.910658Z", "start_time": "2023-09-14T09:09:49.892029Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "False\n", "True\n", "True\n" ] } ], "source": [ "print(\"ring\" in \"strings\") # True\n", "print(\"wow\" in \"amazing!\") # False\n", "print(\"Yes\" in \"yes!\") # False\n", "print(\"\" in \"No way!\") # True\n", "print(\"聪明\" in \"聪明办法学 Python\") # True" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 字符串索引和切片" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "单个字符索引\n", "\n", "索引可以让我们在特定位置找到一个字符" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:52.801994Z", "start_time": "2023-09-14T09:09:52.783931Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Datawhale\n", "D\n", "a\n", "t\n", "a\n" ] } ], "source": [ "s = \"Datawhale\"\n", "print(s)\n", "print(s[0])\n", "print(s[1])\n", "print(s[2])\n", "print(s[3])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:54.714638Z", "start_time": "2023-09-14T09:09:54.710629Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:55.822042Z", "start_time": "2023-09-14T09:09:55.812017Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e\n" ] } ], "source": [ "print(s[len(s)-1])" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:56.927440Z", "start_time": "2023-09-14T09:09:56.902804Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndexError", "evalue": "string index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn [36], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43ms\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m)\n", "\u001b[1;31mIndexError\u001b[0m: string index out of range" ] } ], "source": [ "print(s[len(s)])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "负数索引" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:09:58.771396Z", "start_time": "2023-09-14T09:09:58.751745Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Datawhale\n", "w\n", "h\n", "a\n", "l\n", "e\n" ] } ], "source": [ "print(s)\n", "print(s[-5])\n", "print(s[-4])\n", "print(s[-3])\n", "print(s[-2])\n", "print(s[-1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "用切片来获取字符串的一部分" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:10:00.515352Z", "start_time": "2023-09-14T09:10:00.508269Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data\n", "whale\n" ] } ], "source": [ "print(s[0:4])\n", "print(s[4:9])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:10:01.783542Z", "start_time": "2023-09-14T09:10:01.774947Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Da\n", "ta\n", "ha\n", "le\n" ] } ], "source": [ "print(s[0:2])\n", "print(s[2:4])\n", "print(s[5:7])\n", "print(s[7:9])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "切片的默认参数" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:05:34.573951Z", "start_time": "2023-05-18T11:05:34.565938Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data\n", "whale\n", "Datawhale\n" ] } ], "source": [ "print(s[:4])\n", "print(s[4:])\n", "print(s[:])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "切片的第三个参数 `step`" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:07:15.613361Z", "start_time": "2023-05-18T11:07:15.597348Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Daa\n", "aa\n" ] } ], "source": [ "print(s[:9:3])\n", "print(s[1:4:2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "翻转字符串" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:09:23.334713Z", "start_time": "2023-05-18T11:09:23.310710Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "elahwataD\n" ] } ], "source": [ "# 可以,但是不优雅\n", "print(s[::-1])" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:10:11.219671Z", "start_time": "2023-05-18T11:10:11.203650Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "elahwataD\n" ] } ], "source": [ "# 也可以,但是还是不够优雅\n", "print(\"\".join(reversed(s)))" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:11:29.389867Z", "start_time": "2023-05-18T11:11:29.381840Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "elahwataD\n" ] } ], "source": [ "# 实在是太优雅辣\n", "def reverseString(s):\n", " return s[::-1]\n", "\n", "print(reverseString(s))\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# 字符串的循环" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "用索引的 for 循环" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:12:32.914355Z", "start_time": "2023-05-18T11:12:32.898357Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 D\n", "1 a\n", "2 t\n", "3 a\n", "4 w\n", "5 h\n", "6 a\n", "7 l\n", "8 e\n" ] } ], "source": [ "for i in range(len(s)):\n", " print(i, s[i])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "其实也可以不用索引(超级好用的 `in`)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:13:16.499371Z", "start_time": "2023-05-18T11:13:16.483372Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D\n", "a\n", "t\n", "a\n", "w\n", "h\n", "a\n", "l\n", "e\n" ] } ], "source": [ "for c in s:\n", " print(c)" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:11:13.403586Z", "start_time": "2023-09-14T09:11:13.397556Z" }, "slideshow": { "slide_type": "subslide" } }, "source": [ "也可以使用 `enumerate()` 获得元素的序号" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 D\n", "1 a\n", "2 t\n", "3 a\n", "4 w\n", "5 h\n", "6 a\n", "7 l\n", "8 e\n" ] } ], "source": [ "for idx, c in enumerate(s):\n", " print(idx, c)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`zip(a, b)` 可以在一次循环中,分别从 `a` 和 `b` 里同时取出一个元素" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D e\n", "a l\n", "t a\n", "a h\n", "w w\n", "h a\n", "a t\n", "l a\n", "e D\n" ] } ], "source": [ "for a, b in zip(s, reverseString(s)):\n", " print(a, b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "用 `split()` 来循环" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:14:18.418258Z", "start_time": "2023-05-18T11:14:18.402254Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "learn\n", "python\n", "the\n", "smart\n", "way\n", "2nd\n", "edition\n" ] } ], "source": [ "# class_name.split() 本身会产生一个新的叫做“列表”的东西,但是它不存储任何内容\n", "\n", "class_name = \"learn python the smart way 2nd edition\"\n", "for word in class_name.split():\n", " print(word)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "用 `splitlines()` 来循环" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:15:26.429468Z", "start_time": "2023-05-18T11:15:26.413444Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Prepare To Be Smart, 我们希望同学们学习这个教程后能学习到聪明的办法,从容的迈入人工智能的后续学习。\n" ] } ], "source": [ "# 跟上面一样,class_info.splitlines() 也会产生一个列表,但不存储任何内容\n", "\n", "class_info = \"\"\"\\\n", "聪明办法学 Python 第二版是 Datawhale 基于第一版教程的一次大幅更新。我们尝试在教程中融入更多计算机科学与人工智能相关的内容,制作“面向人工智能的 Python 专项教程”。\n", "\n", "我们的课程简称为 P2S,有两个含义:\n", "\n", "Learn Python The Smart Way V2,“聪明办法学 Python 第二版”的缩写。\n", "Prepare To Be Smart, 我们希望同学们学习这个教程后能学习到聪明的办法,从容的迈入人工智能的后续学习。\n", "\"\"\"\n", "for line in class_info.splitlines():\n", " if (line.startswith(\"Prepare To Be Smart\")):\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# 例子:回文判断" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果一个句子正着读、反着读都是一样的,那它就叫做“回文”" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:17:14.547945Z", "start_time": "2023-05-18T11:17:14.523941Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def isPalindrome1(s):\n", " return (s == reverseString(s))" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:19:30.818148Z", "start_time": "2023-05-18T11:19:30.802467Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def isPalindrome2(s):\n", " for i in range(len(s)):\n", " if (s[i] != s[len(s)-1-i]):\n", " return False\n", " return True" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:19:32.028016Z", "start_time": "2023-05-18T11:19:32.019504Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def isPalindrome3(s):\n", " for i in range(len(s)):\n", " if (s[i] != s[-1-i]):\n", " return False\n", " return True" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:19:33.489527Z", "start_time": "2023-05-18T11:19:33.473410Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def isPalindrome4(s):\n", " while (len(s) > 1):\n", " if (s[0] != s[-1]):\n", " return False\n", " s = s[1:-1]\n", " return True" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:19:34.763872Z", "start_time": "2023-05-18T11:19:34.747861Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n", "True False\n", "True False\n", "True False\n" ] } ], "source": [ "print(isPalindrome1(\"abcba\"), isPalindrome1(\"abca\"))\n", "print(isPalindrome2(\"abcba\"), isPalindrome2(\"abca\"))\n", "print(isPalindrome3(\"abcba\"), isPalindrome3(\"abca\"))\n", "print(isPalindrome4(\"abcba\"), isPalindrome4(\"abca\"))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# 一些跟字符串相关的内置函数" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`str()` 和 `len()`\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:13:33.733039Z", "start_time": "2023-09-14T09:13:30.147004Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入你的名字: Datawhale\n", "Hi, Datawhale, 你的名字有 9 个字!\n" ] } ], "source": [ "name = input(\"输入你的名字: \")\n", "print(\"Hi, \" + name + \", 你的名字有 \" + str(len(name)) + \" 个字!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`chr()` 和 `ord()`\n" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:21:11.615355Z", "start_time": "2023-05-18T11:21:11.607706Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65\n" ] } ], "source": [ "print(ord(\"A\"))" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:21:32.966529Z", "start_time": "2023-05-18T11:21:32.950524Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n" ] } ], "source": [ "print(chr(65))" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:22:08.004859Z", "start_time": "2023-05-18T11:22:07.988839Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B\n" ] } ], "source": [ "print(\n", " chr(\n", " ord(\"A\") + 1\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "ExecuteTime": { "end_time": "2023-05-18T11:23:17.057398Z", "start_time": "2023-05-18T11:23:17.041398Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n" ] } ], "source": [ "print(chr(ord(\"A\") + ord(\" \")))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:14:09.392607Z", "start_time": "2023-09-14T09:14:09.381572Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0\n" ] } ], "source": [ "# 它可以正常运行,但是我们不推荐你使用这个方法\n", "s = \"(3**2 + 4**2)**0.5\"\n", "print(eval(s))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:29:07.725053Z", "start_time": "2023-09-14T09:29:07.715056Z" }, "init_cell": true, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "def 电脑当场爆炸():\n", " from rich.progress import (\n", " Progress, \n", " TextColumn, \n", " BarColumn, \n", " TimeRemainingColumn)\n", " import time\n", " from rich.markdown import Markdown\n", " from rich import print as rprint\n", " from rich.panel import Panel\n", "\n", "\n", " with Progress(TextColumn(\"[progress.description]{task.description}\"),\n", " BarColumn(),\n", " TimeRemainingColumn()) as progress:\n", " epoch_tqdm = progress.add_task(description=\"爆炸倒计时!\", total=100)\n", " for ep in range(100):\n", " time.sleep(0.1)\n", " progress.advance(epoch_tqdm, advance=1)\n", "\n", " rprint(Panel.fit(\"[red]Boom! R.I.P\"))\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "ExecuteTime": { "end_time": "2023-09-14T09:14:22.287390Z", "start_time": "2023-09-14T09:14:11.300311Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "爆炸倒计时! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 0:00:01\n", "\n" ], "text/plain": [ "爆炸倒计时! \u001b[38;2;249;38;114m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[38;2;249;38;114m╸\u001b[0m \u001b[36m0:00:01\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n" ], "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
╭─────────────╮\n",
"│ Boom! R.I.P │\n",
"╰─────────────╯\n",
"
\n"
],
"text/plain": [
"╭─────────────╮\n",
"│ \u001b[31mBoom! R.I.P\u001b[0m │\n",
"╰─────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"s = \"电脑当场爆炸()\"\n",
"eval(s) # 如果这是一串让电脑爆炸的恶意代码,那会发生什么"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"ExecuteTime": {
"end_time": "2023-09-14T09:14:28.579513Z",
"start_time": "2023-09-14T09:14:28.565190Z"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['p', 2, 's']\n",
"