码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础-python数据类型之字符串(四)

时间:2018-12-13 22:41:37      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:常用   dex   iss   切片   结果   dac   bec   inf   操作   

字符串

  字符串是python中常用的数据类型,使用(‘或")来创建。

  创建字符串

    技术分享图片

下标索引

  字符串实际上是字符的数组,所以也支持索引。

  str1 = ‘abcdef‘

  技术分享图片

切片

  定义:切片是指对操作的对象截取一部分的操作。字符串、列表、元祖都支持切片操作。

  语法格式:[起始值:终值:步长]

  附注:切片选取的区间属于左闭右开,即从‘起始值‘到‘终值‘的前一位(不包含终值本身)

  如:  

技术分享图片
1 str1=abcdef
2 print(str1[0:3]=,str1[0:3]) #取出 0~3 的字符,不包含3
3 print(str1[2:4]=,str1[2:4]) #取出 2~4 的字符
4 print(str1[2:]=,str1[2:])   #取出 2~最后 的字符
5 print(str1[:-1]=,str1[:-1]) #取出 0~最后 的字符,不包括最后一个
6 print(str1[:]=,str1[:])     #取出所有字符
7 print(str1[5:1]=,str1[5:1]) #输出为空
字符串-切片

  输出结果为

  技术分享图片

  步长 

技术分享图片
1 str1=abcdef
2 print(str1[0::3]=,str1[0::3]) #所有范围的字符,每次前进两个字符
3 print(str1[0:4:2]=,str1[0:4:2]) #下标0~4之间,每次前进两个字符
4 print(str1[len(str1)::-1]=,str1[len(str1)::-1]) #倒叙输出
字符串-步长

  输出结果为:

  技术分享图片

字符串常见操作

  1、find

    检测指定字符串是否包含在当前字符串中,如果是返回开始的索引,否则返回-1。

    格式:mystr.find(str,start,end).

    如:

技术分享图片
1 str1=How are you,How are you
2 index1=str1.find(How) #查询满足的第一个的下标
3 index2=str1.find(me)  #查询是否有,没有返回-1
4 index3=str1.find(How,12,22) #从指定区间内查询
5 print(index1={},index2={},index3={}.format(index1,index2,index3))
字符串-find

    输出结果为:

    技术分享图片

  2、index

    定义:与find()函数一样,不同之处在于如果要查询的对象不在当前对象中,会报一个异常。

    格式:mystr.index(str,start,end)

    如: 

技术分享图片
1 str1=How are you,How are you
2 index1=str1.index(How) #查询满足的第一个的下标
3 #使用try-except抛出异常
4 try:
5     index2=str1.index(me)  #查询是否有,没有返回一个异常
6 except Exception as e:
7     print(e)
8 index3=str1.index(How,12,22) #从指定区间内查询
9 print(index1={},index3={}.format(index1,index3))
字符串-index

    输出结果为:

    技术分享图片

  3、count

    定义:返回str在start和end之间出现的次数。

    格式:mystr.count(str)

    如:   

技术分享图片
1 str1=How are you,How are you
2 count1=str1.count(How) #查询‘How‘出现的次数
3 print(count1) # 2
字符串-统计次数

  4、replace

    定义:将当前对象的某部分替换为另外一部分。

    格式:mystr.replace(old,new,count=None)

    如:  

技术分享图片
1 str1=How are you,How are you
2 replace1=str1.replace(How,What,count=1) #将How替换为What,替换1次
3 print(replace1) # 输出为‘What are you,How are you‘
字符串-替换

  5、split

    定义:将当前对象按照某种格式或某个字符分割

    格式:mystr.split(str,maxsplit),以str为分割符切片mystr,如果maxsplit有指定数字,则分割maxsplit次。

    如:

技术分享图片
1 str1=How are you,How are you
2 replace1=str1.split( ,maxsplit=3) #将str1以空格分割,分割3次
3 print(replace1) # 输出为[‘How‘, ‘are‘, ‘you,How‘, ‘are you‘]
字符串-分割

  6、capitalize

    定义:把字符串首字母大写

    格式:mystr.capitalize()

    如:

技术分享图片
1 str1=hello
2 capitalize1=str1.capitalize() #将str1的首字母大写
3 print(capitalize1) # 输出为‘Hello‘
字符串-首字母大写

  7、title

    定义:字符串的每个单词的首字母都大写

    格式:mystr.title()

    如:

技术分享图片
1 str1=How are you,How are you
2 replace1=str1.title() #将每个单词的首字母大写
3 print(replace1) # 输出为How Are You,How Are You
字符串-每个首字母大写

   8、startswith

    定义:检查字符串是否是以指定字符串开头, 是则返回 True,否则返回 False 

技术分享图片
1 str1=abcdef
2 print(str1.startswith(a))
3 print(str1.startswith(b))
View Code

  9、endswith

    定义:检查字符串是否以指定字符串结束,如果是返回True,否则返回 False。 

技术分享图片
1 str1=abcdef
2 print(str1.endswith(f))
3 print(str1.endswith(a))
View Code

  10、lower

    定义:将字符串转换为小写。

技术分享图片
1 str1=abcdeF
2 print(str1.lower())
View Code

  11、upper

    定义:将字符串转换为小写

技术分享图片
1 str1=abcdeF
2 print(str1.upper())
View Code

  12、ljust

    定义:返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

    格式:mystr.ljust(参数1,参数2);参数1:填充的个数,参数2:填充的字符

技术分享图片
1 str1=abcdeF
2 print(str1.ljust(9,*))
View Code

  13、rjust

    定义:返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

    格式:mystr.rjust(参数1,参数2);参数1:填充的个数,参数2:填充的字符 

技术分享图片
1 str1=abcdeF
2 print(str1.rjust(9,*))
View Code

  14、center

    定义:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

    格式:mystr.center(参数1,参数2);参数1:填充的个数,参数2:填充的字符  

技术分享图片
1 str1=abcdeF
2 print(str1.center(9,#))
View Code

  15、lstrip

    定义:删除字符串左边的空白字符。

    格式:mystr.lstrip()

技术分享图片
1 str1=     abcdeF
2 print(str1.lstrip())
View Code

  16、rstrip

    定义:删除字符串末尾的空白字符

    格式:mystr.rstrip()

技术分享图片
1 str1=     abcdeF        
2 print(str1.rstrip())
View Code

  17、strip

    定义:删除字符串两端的空白字符

    格式:mystr.strip()  

技术分享图片
1 str1=     abcdeF        
2 print(str1.strip())
View Code

  18、rfind

    定义:类似find()函数,不过是从右侧开始查找。返回最后一次出现的字符,找不到返回-1

    格式:mystr.rfind(参数1,参数2,参数3);参数1:查询的字符,参数2:开始查找的位置,默认为0,参数3:结束查找的位置 

技术分享图片
1 str1=abcdeF a
2 print(str1.rfind(a,7,0))
View Code

  19、rindex

    定义:类似index()函数,不过是从左侧开始查找。找不到返回一个异常

    格式:mystr.rindex(参数1,参数2,参数3);参数1:查询的字符,参数2:开始查找的位置,默认为0,参数3:结束查找的位置  

技术分享图片
1 try:
2     str1=abcdeF a
3     print(str1.rindex(a,7,0))
4 except Exception as e:
5     print(e)
View Code

  20、isspace

    定义:如果字符串只包含空格,则返回True,否则返回False。

    格式:mystr.isspace()   

技术分享图片
1 str1=abcdeF a
2 print(str1.isspace())
View Code

 

 

 

        

 

    

 

 

 

    

 

 

  

 

Python基础-python数据类型之字符串(四)

标签:常用   dex   iss   切片   结果   dac   bec   inf   操作   

原文地址:https://www.cnblogs.com/I-love-Xiang/p/10111265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!