🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 字符串常见操作 如有字符串`mystr = 'hello world itcast and itcastcpp'`,以下是常见的操作 #### 1\. find 检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1 ~~~ mystr.find(str, start=0, end=len(mystr)) ~~~ ![](https://img.kancloud.cn/9a/f2/9af2c9b715c14fd032dfa77e11c4aec4_708x112.png) ![](https://img.kancloud.cn/c9/41/c941decc09766f1984dcef57eff8618f_714x111.png) #### 2\. index 跟find()方法一样,只不过如果str不在 mystr中会报一个异常. ~~~ mystr.index(str, start=0, end=len(mystr)) ~~~ ![](https://img.kancloud.cn/eb/d1/ebd106c734595c22489c8cc01ae6ec80_710x235.png) #### 3\. count 返回 str在start和end之间 在 mystr里面出现的次数 ~~~ mystr.count(str, start=0, end=len(mystr)) ~~~ ![](https://img.kancloud.cn/6e/2b/6e2b727d8a3b2e1e66d7af1121f580ab_740x113.png) #### 4\. replace 把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次. ~~~ mystr.replace(str1, str2, mystr.count(str1)) ~~~ ![](https://img.kancloud.cn/fa/69/fa69d208c157358cb0f108e5df484704_508x179.png) #### 5\. split 以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串 ~~~ mystr.split(str=" ", 2) ~~~ ![](https://img.kancloud.cn/f9/20/f9204d7a74772dcaf2f12c310a814b5d_534x178.png) #### 6\. capitalize 把字符串的第一个字符大写 ~~~ mystr.capitalize() ~~~ ![](https://img.kancloud.cn/21/ce/21ceff8a94bb3b1f5bd7d7f03612c8a6_744x119.png) #### 7\. title 把字符串的每个单词首字母大写 ~~~ >>> a = "hello itcast" >>> a.title() 'Hello Itcast' ~~~ #### 8\. startswith 检查字符串是否是以 hello 开头, 是则返回 True,否则返回 False ~~~ mystr.startswith(hello) ~~~ ![](https://img.kancloud.cn/2c/13/2c130348d7e75ddefcee21ac4ed636c8_797x176.png) #### 9\. endswith 检查字符串是否以obj结束,如果是返回True,否则返回 False. ~~~ mystr.endswith(obj) ~~~ ![](https://img.kancloud.cn/64/2f/642f3410b357731dde23ee880ab4bb7e_725x172.png) #### 10\. lower 转换 mystr 中所有大写字符为小写 ~~~ mystr.lower() ~~~ ![](https://img.kancloud.cn/3d/30/3d303bb4a5ca4b1d0afb9594e66a900f_717x119.png) #### 11\. upper 转换 mystr 中的小写字母为大写 ~~~ mystr.upper() ~~~ ![](https://img.kancloud.cn/0c/c6/0cc6cebd255a237ffcc69dbadf1b5c8a_731x122.png) #### 12\. lstrip 删除 mystr 左边的空白字符 ~~~ mystr.lstrip() ~~~ ![](https://img.kancloud.cn/d7/b6/d7b6c934926e90493dec4c940c5418b4_546x206.png) #### 13\. rstrip 删除 mystr 字符串末尾的空白字符 ~~~ mystr.rstrip() ~~~ ![](https://img.kancloud.cn/e2/d8/e2d8c39b6c22bee4f59c3c53933c375c_548x114.png) #### 14\. strip 删除mystr字符串两端的空白字符 ~~~ >>> a = "\n\t itcast \t\n" >>> a.strip() 'itcast' ~~~ ### 15\. rfind 类似于 find()函数,不过是从右边开始查找. ~~~ mystr.rfind(str, start=0,end=len(mystr) ) ~~~ ![](https://img.kancloud.cn/39/00/3900c54f64bb4215b46f4de373d4ea48_683x117.png) ### 16\. rindex 类似于 index(),不过是从右边开始. ~~~ mystr.rindex( str, start=0,end=len(mystr)) ~~~ ![](https://img.kancloud.cn/50/7c/507cb67f678c85f203d8b12389987b46_727x175.png) ### 17\. partition 把mystr以str分割成三部分,str前,str和str后 ~~~ mystr.partition(str) ~~~ ![](https://img.kancloud.cn/55/cf/55cf2c90eae6ae682c6940a001fab44e_745x116.png) ### 18\. rpartition 类似于 partition()函数,不过是从右边开始. ~~~ mystr.rpartition(str) ~~~ ![](https://img.kancloud.cn/fb/aa/fbaa358fba1687d962b70db50e6feccf_757x183.png) ### 19\. splitlines 按照行分隔,返回一个包含各行作为元素的列表 ~~~ mystr.splitlines() ~~~ ![](https://img.kancloud.cn/ce/27/ce2756bf9aa7dc526f5e21d94414f1a1_507x202.png) ### 20\. isalpha 如果 mystr 所有字符都是字母 则返回 True,否则返回 False ~~~ mystr.isalpha() ~~~ ![](https://img.kancloud.cn/41/0c/410cc5d71c7d15dfb9745422cf9b974f_367x291.png) ### 21\. isdigit 如果 mystr 只包含数字则返回 True 否则返回 False. ~~~ mystr.isdigit() ~~~ ![](https://img.kancloud.cn/3d/f1/3df1dc0727fa6c9f5dbe48b6c7d58936_398x297.png) ### 22\. isalnum 如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False ~~~ mystr.isalnum() ~~~ ![](https://img.kancloud.cn/c2/aa/c2aa4f043ba7fc1b1a5aea69ab64f8c0_431x385.png) ### 23\. isspace 如果 mystr 中只包含空格,则返回 True,否则返回 False. ~~~ mystr.isspace() ~~~ ![](https://img.kancloud.cn/be/68/be68fcf08942f73c3baac48017b728cf_390x382.png) ### 24\. join mystr 中每个元素后面插入str,构造出一个新的字符串 ~~~ mystr.join(str) ~~~ ![](https://img.kancloud.cn/d1/98/d19841ea3ade0731441f513dd9f81c2a_609x235.png) # 想一想 * (面试题)给定一个字符串aStr,返回使用空格或者'\\t'分割后的倒数第二个子串 ![![](../Images/Snip20161225_1.png)](images/screenshot_1593859329967.png)