盘点Python字符串常见的16种操作方法

大家好,我是Go进阶者,上篇文章给大家介绍了Python字符串,今天给大家分享一些Python字符串的常用操作,一起来看看吧~

一、常用操作

以字符串

 
 
 
 
  1. 'lstr = 'welcome to Beijing Museumitcpps fdsfs' 

为例,介绍字符常见的操作。

<1> find

检测 str 是否包含在 lstr中,如果是返回开始的索引值,否则返回-1。

语法:

 
 
 
 
  1. lstr.find(str, start=0, end=len(lstr)) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2. print(lstr.find("Museum")) 
  3.  
  4. print(lstr.find("dada")) 

运行结果:

<2> index

跟find()方法一样,只不过如果str不在 lstr中会报一个异常。

语法:

 
 
 
 
  1. lstr.index(str, start=0, end=len(lstr)) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2.  
  3. print(lstr.index("dada")) 

运行结果:

<3> count

返回 str在start和end之间 在 lstr里面出现的次数

语法:

 
 
 
 
  1. lstr.count(str, start=0, end=len(lstr)) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.count("s")) 

运行结果:

<4> replace

把 lstr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

 
 
 
 
  1. 1str.replace(str1, str2,  1str.count(str1)) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.replace("s", "ttennd")) 

运行结果:

<5> split

以 str 为分隔符切片 lstr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

 
 
 
 
  1. 1str.split(str=" ", 2) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.split("to", 5)) 

运行结果:

<6> capitalize

把字符串的第一个字符大写。

 
 
 
 
  1. lstr.capitalize() 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.capitalize()) 

运行结果:

<7> title

把字符串的每个单词首字母大写。

 
 
 
 
  1. >>> a = "hello itcast" 
  2. >>> a.title() 
  3. 'Hello Itcast' #运行结果 

<8> startswith

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

 
 
 
 
  1. 1str.startswith(obj) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.startswith('we')) 

运行结果:

<9> endswith

检查字符串是否以obj结束,如果是返回True,否则返回 False.

 
 
 
 
  1. 1str.endswith(obj) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.endswith('hfs')) 

运行结果:

<10> lower

转换 lstr 中所有大写字符为小写

 
 
 
 
  1. 1str.lower() 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.lower()) 

运行结果:

<11> upper

转换 lstr 中的小写字母为大写

 
 
 
 
  1. 1str.upper() 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.upper()) 

运行结果:

<12> strip

删除lstr字符串两端的空白字符。

 
 
 
 
  1. >>> a = "\n\t itcast \t\n" 
  2. >>> a.strip() 
  3. 'itcast'  #运行结果 

<13> rfind

类似于 find()函数,不过是从右边开始查找。

 
 
 
 
  1. 1str.rfind(str, start=0,end=len(1str) ) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rfind('eijing')) 

运行结果:

<14> rindex

类似于 index(),不过是从右边开始。

 
 
 
 
  1. 1str.rindex( str, start=0,end=len(1str)) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rindex('eijing')) 

运行结果:

<15> partition

把lstr以str分割成三部分,str前,str和str后。

 
 
 
 
  1. 1str.partition(str) 

例:

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.partition('eijing')) 

运行结果:

<16> join

mystr 中每个字符后面插入str,构造出一个新的字符串。

 
 
 
 
  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. str='233' 
  3. lstr.join(str) 
  4. li=["my","name","is","LY"] 
  5. print(str.join(li)) 

运行结果:

二、总结

本文详细的讲解了Python基础 ( 字符串 )。介绍了有关字符串,切片的操作。下标索引。以及在实际操作中会遇到的问题,提供了解决方案。希望可以帮助你更好的学习Python。

文章题目:盘点Python字符串常见的16种操作方法
路径分享:http://www.shufengxianlan.com/qtweb/news43/322593.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联