{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 字符串操作" ] }, { "cell_type": "markdown", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "标准**序列操作**适用于字符串\n", "\n", "- 索引,分片\n", "- 乘法,加法\n", "- 成员资格判断\n", "- 求长度,最大值,最小值\n", "\n", "字符串**不可变**,因此**分片赋值**或**单独赋值**是不合法的" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在某种程度上,可以把字符串看做一个字符列表" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下列示例字符串选自由孙燕姿演唱的歌曲《我怀念的》,姚若龙 作词,戳我观看MV\n", "\n", "\n", "\n", "我怀念的是无话不说\n", "\n", "我怀念的是一起作梦\n", "\n", "我怀念的是争吵以后\n", "\n", "还是想要爱你的冲动" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下列示例字符串选自由伍佰&杨乃文演唱的歌曲《最初的地方》,余光中 作词,戳我观看现场视频\n", "\n", "回到最初我们来的地方\n", "\n", "好多的天真被我们遗忘\n", "\n", "你说过快乐就在这路上\n", "\n", "从不在乎结果究竟会怎样" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "lyrics_1 = \"我怀念的是无话不说\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'念'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 按索引访问\n", "lyrics_1[2]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'无话不说'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 分片\n", "lyrics_1[-4:]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 成员判断\n", "'我' in lyrics_1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 成员判断\n", "'无话不说' in lyrics_1" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'说不话无是的念怀我'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 倒序\n", "lyrics_1[::-1]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'我怀念的是无话不说我怀念的是无话不说'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 乘法\n", "\n", "lyrics_1 * 2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'我怀念的是无话不说,我怀念的是一起作梦'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 加法\n", "\n", "lyrics_1 + ',' +\"我怀念的是一起作梦\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'我怀念的,我怀念的,是无话不说'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 重复和扩展(乘法和加法)\n", "\n", "'我怀念的,' * 2 + '是无话不说'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 求长度\n", "\n", "len(lyrics_1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'说'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 最大值\n", "\n", "max(lyrics_1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'不'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 最小值\n", "\n", "min(lyrics_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "思考题:请**用代码**解释`max(lyrics_1)`和`min(lyrics_1)`的执行过程" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# 字符串不可变,单独赋值不行,不行,不行\n", "# lyrics_1[0] = '你'" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# 字符串不可变,切片赋值不行,不行,不行\n", "# lyrics_1[1:3] = '想念' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 字符串常用方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "先用内置函数`help`查看一下字符串的所有方法:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'go on '" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\"[14:20]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "str1 = '喜欢看你紧紧皱眉 叫我胆小鬼'" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'胆小鬼'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str1[-3:15]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# help(str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "接下来咱们看看有哪些常用方法:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法`center`通过在**两边**添加填充字符(默认为空格)让字符串**居中**" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 回到最初我们来的地方 '" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"回到最初我们来的地方\".center(20)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'~~~~~回到最初我们来的地方~~~~~'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"回到最初我们来的地方\".center(20,'~')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法`find`在字符串中查找子串。如果找到,就**返回**子串的**第一个字符的索引**,否则返回-1。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"回到最初我们来的地方\".find('我们')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'我'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"回到最初我们来的地方\"[4]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 返回第一次出现时的索引值,即使多次出现了\n", "\"My heart will go on and on\".find('on')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 指定查找的范围,即子串[14:20],返回结果为17,这个结果的索引是在整个字符串中的索引,而不是在子串中的索引\n", "\"My heart will go on and on\".find('on',14,20)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\".find('wi')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\".find('wi',14,20)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\"[17]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "26" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(\"My heart will go on and on\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\"[24]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 没找到,返回-1\n", "\"My heart will go on and on\".find('you')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下歌词来自《My Heart Will Go On》,由Will Jennings作词,戳我观看MV\n", "\n", "```\n", "Every night in my dreams\n", "I see you, I feel you,\n", "That is how I know you go on\n", "\n", "Far across the distance\n", "And spaces between us\n", "You have come to show you go on\n", "\n", "Near, far, wherever you are\n", "I believe that the heart does go on\n", "Once more you open the door\n", "And you are here in my heart\n", "And my heart will go on and on\n", "\n", "Love can touch us one time\n", "And last for a lifetime\n", "And never let go till we are gone\n", "\n", "Love was when I loved you\n", "One true time I hold to\n", "In my life we will always go on\n", "\n", "Near, far, wherever you are\n", "I believe that the heart does go on\n", "Once more you open the door\n", "And you are here in my heart\n", "And my heart will go on and on\n", "\n", "You are here, there is nothing I fear,\n", "And I know that my heart will go on\n", "We will stay forever this way\n", "You are safe in my heart\n", "And my heart will go on and on\n", "```" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "lyrics = '''Every night in my dreams\n", "I see you, I feel you,\n", "That is how I know you go on\n", "\n", "Far across the distance\n", "And spaces between us\n", "You have come to show you go on\n", "\n", "Near, far, wherever you are\n", "I believe that the heart does go on\n", "Once more you open the door\n", "And you are here in my heart\n", "And my heart will go on and on\n", "\n", "Love can touch us one time\n", "And last for a lifetime\n", "And never let go till we are gone\n", "\n", "Love was when I loved you\n", "One true time I hold to\n", "In my life we will always go on\n", "\n", "Near, far, wherever you are\n", "I believe that the heart does go on\n", "Once more you open the door\n", "And you are here in my heart\n", "And my heart will go on and on\n", "\n", "You are here, there is nothing I fear,\n", "And I know that my heart will go on\n", "We will stay forever this way\n", "You are safe in my heart\n", "And my heart will go on and on'''" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Every night in my dreams\\nI see you, I feel you,\\nThat is how I know you go on\\n\\nFar across the distance\\nAnd spaces between us\\nYou have come to show you go on\\n\\nNear, far, wherever you are\\nI believe that the heart does go on\\nOnce more you open the door\\nAnd you are here in my heart\\nAnd my heart will go on and on\\n\\nLove can touch us one time\\nAnd last for a lifetime\\nAnd never let go till we are gone\\n\\nLove was when I loved you\\nOne true time I hold to\\nIn my life we will always go on\\n\\nNear, far, wherever you are\\nI believe that the heart does go on\\nOnce more you open the door\\nAnd you are here in my heart\\nAnd my heart will go on and on\\n\\nYou are here, there is nothing I fear,\\nAnd I know that my heart will go on\\nWe will stay forever this way\\nYou are safe in my heart\\nAnd my heart will go on and on'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lyrics" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Every night in my dreams I see you, I feel you, That is how I know you go on Far across the distance And spaces between us You have come to show you go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on Love can touch us one time And last for a lifetime And never let go till we are gone Love was when I loved you One true time I hold to In my life we will always go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on You are here, there is nothing I fear, And I know that my heart will go on We will stay forever this way You are safe in my heart And my heart will go on and on'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 把换行符\\n替换为空格\n", "lyrics.replace('\\n',' ')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "lyrics_str = lyrics.replace('\\n',' ')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# 使用split函数把歌词字符串转换为单词列表\n", "lyrics_list = lyrics_str.split(' ')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Every', 'night', 'in', 'my', 'dreams', 'I', 'see', 'you,', 'I', 'feel', 'you,', 'That', 'is', 'how', 'I', 'know', 'you', 'go', 'on', '', 'Far', 'across', 'the', 'distance', 'And', 'spaces', 'between', 'us', 'You', 'have', 'come', 'to', 'show', 'you', 'go', 'on', '', 'Near,', 'far,', 'wherever', 'you', 'are', 'I', 'believe', 'that', 'the', 'heart', 'does', 'go', 'on', 'Once', 'more', 'you', 'open', 'the', 'door', 'And', 'you', 'are', 'here', 'in', 'my', 'heart', 'And', 'my', 'heart', 'will', 'go', 'on', 'and', 'on', '', 'Love', 'can', 'touch', 'us', 'one', 'time', 'And', 'last', 'for', 'a', 'lifetime', 'And', 'never', 'let', 'go', 'till', 'we', 'are', 'gone', '', 'Love', 'was', 'when', 'I', 'loved', 'you', 'One', 'true', 'time', 'I', 'hold', 'to', 'In', 'my', 'life', 'we', 'will', 'always', 'go', 'on', '', 'Near,', 'far,', 'wherever', 'you', 'are', 'I', 'believe', 'that', 'the', 'heart', 'does', 'go', 'on', 'Once', 'more', 'you', 'open', 'the', 'door', 'And', 'you', 'are', 'here', 'in', 'my', 'heart', 'And', 'my', 'heart', 'will', 'go', 'on', 'and', 'on', '', 'You', 'are', 'here,', 'there', 'is', 'nothing', 'I', 'fear,', 'And', 'I', 'know', 'that', 'my', 'heart', 'will', 'go', 'on', 'We', 'will', 'stay', 'forever', 'this', 'way', 'You', 'are', 'safe', 'in', 'my', 'heart', 'And', 'my', 'heart', 'will', 'go', 'on', 'and', 'on']\n" ] } ], "source": [ "print(lyrics_list)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Every night in my dreams I see you, I feel you, That is how I know you go on Far across the distance And spaces between us You have come to show you go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on Love can touch us one time And last for a lifetime And never let go till we are gone Love was when I loved you One true time I hold to In my life we will always go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on You are here, there is nothing I fear, And I know that my heart will go on We will stay forever this way You are safe in my heart And my heart will go on and on'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使用join函数将列表合并为字符串\n", "' '.join(lyrics_list)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Every night in my dreams I see you, I feel you, That is how I know you go on Far across the distance And spaces between us You have come to show you go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on Love can touch us one time And last for a lifetime And never let go till we are gone Love was when I loved you One true time I hold to In my life we will always go on Near, far, wherever you are I believe that the heart does go on Once more you open the door And you are here in my heart And my heart will go on and on You are here, there is nothing I fear, And I know that my heart will go on We will stay forever this way You are safe in my heart And my heart will go on and on'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lyrics_str" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['My', 'heart', 'will', 'go', 'on', 'and', 'on']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"My heart will go on and on\".split(' ')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'My~heart~will~go~on~and~on'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'~'.join(['My', 'heart', 'will', 'go', 'on', 'and', 'on'])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "num_str = '1 2 \\r 3\\t4\\n9'" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "num_list = num_str.split()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '9']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_list" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1*2*3*4*9'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'*'.join(num_list)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1*2*3*4*9的计算结果为:216\n" ] } ], "source": [ "print(f\"{'*'.join(num_list)}的计算结果为:{1*2*3*4*9}\")" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'every night in my dreams i see you, i feel you, that is how i know you go on far across the distance and spaces between us you have come to show you go on near, far, wherever you are i believe that the heart does go on once more you open the door and you are here in my heart and my heart will go on and on love can touch us one time and last for a lifetime and never let go till we are gone love was when i loved you one true time i hold to in my life we will always go on near, far, wherever you are i believe that the heart does go on once more you open the door and you are here in my heart and my heart will go on and on you are here, there is nothing i fear, and i know that my heart will go on we will stay forever this way you are safe in my heart and my heart will go on and on'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使用lower函数将字符串全部,全部,全部,变为小写,小写,小写\n", "lyrics_str.lower()" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'EVERY NIGHT IN MY DREAMS I SEE YOU, I FEEL YOU, THAT IS HOW I KNOW YOU GO ON FAR ACROSS THE DISTANCE AND SPACES BETWEEN US YOU HAVE COME TO SHOW YOU GO ON NEAR, FAR, WHEREVER YOU ARE I BELIEVE THAT THE HEART DOES GO ON ONCE MORE YOU OPEN THE DOOR AND YOU ARE HERE IN MY HEART AND MY HEART WILL GO ON AND ON LOVE CAN TOUCH US ONE TIME AND LAST FOR A LIFETIME AND NEVER LET GO TILL WE ARE GONE LOVE WAS WHEN I LOVED YOU ONE TRUE TIME I HOLD TO IN MY LIFE WE WILL ALWAYS GO ON NEAR, FAR, WHEREVER YOU ARE I BELIEVE THAT THE HEART DOES GO ON ONCE MORE YOU OPEN THE DOOR AND YOU ARE HERE IN MY HEART AND MY HEART WILL GO ON AND ON YOU ARE HERE, THERE IS NOTHING I FEAR, AND I KNOW THAT MY HEART WILL GO ON WE WILL STAY FOREVER THIS WAY YOU ARE SAFE IN MY HEART AND MY HEART WILL GO ON AND ON'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使用upper函数将字符串全部,全部,全部,变为大写,大写,大写\n", "lyrics_str.upper()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "lyrics_lower = lyrics_str.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法`replace`将指定子串都替换为另一个字符串,并返回替换后的结果。类似的`translate`方法参考教材练习一下。" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# 贾岛骑驴作诗,最开始想到的词句是\n", "poems = '鸟宿池边树,僧推月下门'" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "# 一边骑驴一边琢磨,敲字好像不合适,换个什么字好呢" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "# 思索的时候,骑驴分神,撞到了时任京兆尹的韩愈的出巡队伍里" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "# 韩愈了解情况后,说,我来给你替换一下,于是便有了下一行代码" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'鸟宿池边树,僧敲月下门'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "poems.replace('推','敲')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'good'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'goooood'.replace('ooo','')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'s~ g~~d'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'so good'.replace('o','~')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'鸟宿池边树,僧推月下门'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "poems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "方法`strip`将字符串**开头和末尾**的空白(但不包括中间的空白)删除,并返回删除后的结果。" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "str_6 = ' 首行缩进2个字符。\\r\\n我是第二行\\n我是第\\t三行。终于写完了,使劲敲下回车\\r\\n'" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 首行缩进2个字符。\n", "我是第二行\n", "我是第\t三行。终于写完了,使劲敲下回车\n", "\n" ] } ], "source": [ "print(str_6)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'首行缩进2个字符。\\r\\n我是第二行\\n我是第\\t三行。终于写完了,使劲敲下回车'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_6.strip()" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 首行缩进2个字符。\\r\\n我是第二行\\n我是第\\t三行。终于写完了,使劲敲下回车\\r\\n'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_6" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "str_7 = ' 首行缩进2个字符。\\r\\n我是第二行\\n我是第\\t三行。终于写完了,使劲敲下回车\\t\\r\\n'" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'首行缩进2个字符。\\r\\n我是第二行\\n我是第\\t三行。终于写完了,使劲敲下回车'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_7.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "空白包含`\\n`,`\\r`,`\\t`,` `,不知包括空格哦 (~ ̄▽ ̄~)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以使用strip删除字符串首尾的指定的(一个或者多个)字符" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "str_8 = 'ababoabcll'" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ababoabc'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.strip('l')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ababoabcll'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'oabcll'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.strip('ab')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`startswith`和`endswith`函数用来判断字符串首尾是否以特定字符(串)开始或结束" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.startswith('a')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.startswith('ab')" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.startswith('aba')" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.endswith('l')" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 指定字符串子串的首尾索引值,可以用来对子串进行首尾字符判断\n", "str_8.endswith('l', 0, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`index`函数返回指定字符(串)在字符串中的第一次出现,第一次出现,第一次出现的索引值" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_8.index('a')" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "# 如果没找到,就会报错哦 (╬▔^▔)凸\n", "# str_8.index('bcd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`count`函数用来计算指定字符(串)在字符串中出现的次数,这个函数很重要,一定要会灵活使用哦,[>\\\\/<] " ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "# 看看下面这段绕口令里出现几次 扁担,几次 板凳\n", "rao_str = '扁担长,板凳宽,板凳没有扁担长,扁担没有板凳宽。扁担要绑在板凳上,板凳偏不让扁担绑在板凳上。'" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rao_str.count('扁担')" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rao_str.count('板凳')" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "th3s 3s str3ng 2x1mpl2....w4w!!!\n" ] } ], "source": [ "intab = \"aeiou\"\n", "outtab = \"12345\"\n", "trantab = str.maketrans(intab, outtab) \n", "\n", "str10 = \"this is string example....wow!!!\"\n", "print (str10.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "不坏\n" ] } ], "source": [ "intab = \"好\"\n", "outtab = \"坏\"\n", "trantab = str.maketrans(intab, outtab) \n", "\n", "str10 = \"不好\"\n", "print (str10.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "# help(str.translate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用字符串的下列`方法`判断字符串是否满足特定的条件:`isalnum`、`isalpha`、`isdecimal`、`isdigit`、`isidentifier`、`islower`、`isnumeric`、`isprintable`、`isspace`、`istitle、isupper`。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用字符串的下列方法判断字符串是否满足特定的条件:isalnum、isalpha、isdecimal、isdigit、isidentifier、islower、isnumeric、isprintable、isspace、istitle、isupper字符串的方法很多,自己一定,一定,一定多练习敲代码哦" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "string模块包含了许多常用字符集\n", "\n", "下面是模块string中几个很有用的常量:\n", "\n", "- `string.digits`:包含数字0~9的字符串。\n", "- `string.ascii_letters`:包含所有ASCII字母(大写和小写)的字符串。\n", "- `string.ascii_lowercase`:包含所有小写ASCII字母的字符串。\n", "- `string.printable`:包含所有可打印的ASCII字符的字符串。\n", "- `string.punctuation`:包含所有ASCII标点字符的字符串。\n", "- `string.ascii_uppercase`:包含所有大写ASCII字母的字符串。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用`string`模块中的这些字符集时,一定要,一定要,一定要,先导入string模块" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "import string" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "# help(string)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0123456789'" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.digits" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.punctuation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "思考题:分析《My Heart Will Go On》的歌词中出现频率最高的5个词语,系动词和标点不算。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**string模块**\n", "\n", "下面是模块string中几个很有用的常量:\n", "\n", "- `string.digits`:包含数字0~9的字符串。\n", "- `string.ascii_letters`:包含所有ASCII字母(大写和小写)的字符串。\n", "- `string.ascii_lowercase`:包含所有小写ASCII字母的字符串。\n", "- `string.printable`:包含所有可打印的ASCII字符的字符串。\n", "- `string.punctuation`:包含所有ASCII标点字符的字符串。\n", "- `string.ascii_uppercase`:包含所有大写ASCII字母的字符串。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用`string`模块中的这些字符集时,一定要,一定要,一定要,先导入string模块" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "import string" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0123456789'" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.digits" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~'" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.punctuation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 常用的处理字符串的内置函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `str()`函数:将值转化为适于人阅读的**字符串**的形式;\n", "\n", "- `repr()`函数:将值转化为供解释器读取的**字符串**形式;\n", "\n", "- `eval()`函数:执行一个字符串表达式,并返回表达式的值。\n", "\n", "拓展学习:python3编程基础:str()、repr()的区别" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "x = 123" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "x_str = str(x)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'123'" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_str" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x_str)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "x_repr = repr(x)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'123'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_repr" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x_repr)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 执行一个字符串表达式\n", "eval(x_str)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 执行一个字符串表达式\n", "eval(x_repr)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(eval(x_str))" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(eval(x_repr))" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "y = '123'" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "y_str = str(y)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'123'" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_str" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "y_repr = repr(y)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "\"'123'\"" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_repr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`str()`和`repr()`都可以把出传入的对象转换为字符串,当传入对象为字符串类型时,`repr()`会再加一层引号;\n", "\n", "`eval()`函数接收**字符串**,先把字符串的**引号去掉**,再把字符串引号里的内容当做表达式执行,并返回执行结果。" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(eval(y_repr))" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(eval(y_str))" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "exp_1 = '+'.join('1234')" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1+2+3+4'" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp_1" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(exp_1)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1+2+3+4'" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(exp_1)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"'1+2+3+4'\"" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(exp_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "思考题:\n", "\n", " - 如果下面运行代码`eval(str(exp_1))`,结果是什么\n", " - 如果下面运行代码`eval(repr(exp_1))`,结果是什么" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 转义字符" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "有些字符**无法直接输出**,可以通过`\\`转义**其它字符**实现。你可以把下表理解为,在Python中表示**特殊字符**的汇总表。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "