字符串的常用方法(内建函数)

之前提到过,方法就是针对特殊对象的函数,序列有许多方法,元祖也有,同样的,字符串也有其特有的方法。

序号 函数   功能                                          
1 find()  
2 split()  
3 join()  
4 lower()  
5 title()  
6 replace()  
7 index()  
8 capitalize()  
9 upper()  
10 swapcase()  
11 center()  
12 startswith()  
13 endswith()  
14 expandtabs()  
15 strip()、lstrip()、rstrip()  
16 format()  
15 isalnum()、isalpha()、isdigit()  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.find():可以在一个较长的字符串中查找字串。它返回字串所在位置的最左端索引,如果没有找到,则返回-1.

>>> st = 'Come here, quiet and simple'  #淡泊明志,宁静致远
>>> st.find('here')
5
>>> st.find('most')
-1

还可以指定查找的范围。

>>> st.find('here',0,20)
5
>>> st.find('here',0,6)    #单个数字表示起始位置
-1

2.split():将字符串分割成列表。

>>> '1+2+3+4'.split("+")
['1', '2', '3', '4']
>>> 'Using the default'.split()
['Using', 'the', 'default'] #如果不提供分割符,默认空格作为分割符

还可以指定分割的次数:

>>> sst1 = 'This is the best of times'
>>> sst1.split()
['This', 'is', 'the', 'best', 'of', 'times']

>>> sst1.split(' ',3)    #分割三次
['This', 'is', 'the', 'best of times']
>>> sst1.split(' ',100)    #分割次数大于实际最大次数时,按照实际为主。
['This', 'is', 'the', 'best', 'of', 'times']
>>> sst2 = 'title tle tlie'
>>> sst2.split('t',3)
['', 'i', 'le ', 'le tlie']  #以首字符作为分隔符的时候,注意会产生一个空字符串

 

注意:分割次数大于实际次数时不会报错,这样可以节省内存空间。把一切的异常指向最大值,这样会省许多麻烦。

3.join():split()的逆方法,用来连接序列中的元素。

>>> number
[1, 2, 3, 4]
>>> sep = '+'     #必须指定连接符号
>>> sep.join(number)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found

>>> number = ['1','2','3','4']   #需要被连接的序列元素必须是字符串
>>> print(seo.join(number))
1234

>>> name
['wo', 'shi', 'shui']
>>> print(" ".join(number))
1 2 3 4

4.lower():返回字符串的小写字母表。在不想区分大小写的地方十分的受用。

Name = ['kebi','maoxian','xiaoniao','xinye']
if input('please enter your name: ').lower() in Name:
    print('Found it')

please enter your name: KEBI
Found it

 

5.title():将字符串转换为标题,也就是单词首字母大写,而其余的小写。

>>> name = 'wo shi shui?'
>>> name.title()
'Wo Shi Shui?'
>>> ss = 'WO SHI SHUI?'  #不论大小写
>>> ss.title()
'Wo Shi Shui?'

与title():类似的还有string模块的capwords函数。

>>> import string
>>> string.capwords("that's all folks")
"That's All Folks"

>>> string.capwords("all of and")
'All Of And'

6.replace():返回某字符串的所有匹配项均被替换之后得到的字符串

>>> 'This is a test'.replace('is','IS')   #只能一对一替换
'ThIS IS a test'
>>> 'This is a test'.replace('t','H')
'This is a HesH'

 

>>> 'my stster is my father of my motherof son'.replace('my','I')
'I stster is I father of I motherof son'

>>> 'my stster is my father of my motherof son'.replace('my','I',1)
'I stster is my father of my motherof son' #可以指定替换的次数

>>> 'my stster is my father of my motherof son'.replace('my','I',100)  #替换次数高于实际次数时不会报错
'I stster is I father of I motherof son'

 

7.index():从序列中查找某个元素的第一个匹配项的索引位置,没有查找到就报错。

     与find()类似,但是find()只针对字符串有效,而find()对序列都有效。

>>> st = 'Hello,World'
>>> st.index('He')
0
>>> st.index('o')
4
>>> st.index('x')     #index没有就报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

>>> st.find('GG')    #find没有则返回-1
-1

>>> st1 = [1,2,3,4]
>>> st2 = [5,6,7,8]
>>> st2 = (5,6,7,8)
>>> st1.index(2)
1
>>> st2.index(6)      #index可以作用于序列
1
>>> st2.find(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'find'

8.capitalize():将字符串的首字母大写,其余的不论大小写,全部小写。

       与title()有些类似,但是又不同,title()将每个单词的首字母大写。

>>> st
'Hello,World'
>>> st = 'hello,world'
>>> st.capitalize()
'Hello,world'     #只将整个字符串的第一个字母大写
>>> st.title()
'Hello,World'   #每个单词首字母都变成大写
>>> st1 = 'hEloO,wORRD'
>>> st1.capitalize()
'Heloo,worrd'
>>> st1.title()
'Heloo,Worrd'

9.upper():将所有字母大写,与lower()相对应

>>> st
'hello,world'
>>> st1
'hEloO,wORRD'
>>> st.upper()
'HELLO,WORLD'
>>> st1.upper()
'HELOO,WORRD'

10.swapcase():大小写翻转

>>> st
'hello,world'
>>> st1
'hEloO,wORRD'
>>> st.swapcase()
'HELLO,WORLD'
>>> st1.swapcase()
'HeLOo,Worrd'

11.center():返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。

>>> name
'Kebi'
>>> name.center(20)  #一定要指定宽度,可以不指定填充符号
'        Kebi        '
>>> name.center(20,'*')
'********Kebi********'

12.startswith():判定字符串是否是以指定的字符开头,返回布尔值,可以指定检测范围。

>>> st
'hello,world'
>>> st.startswith('he')
True
>>> st.startswith('the')
False

>>> st.startswith('wo',6)  #从第7个字符开始查,也就是w
True

13.endswith():判断字符串是否以指定的字符结尾,返回布尔值,可以指定检测范围

>>> st.endswith('o',0,5)
True

14.expandtabs():默认将一个tab键变成8个宽度,如果tab前面的不足八个,则补全。如果超过8个则补全到16个,而tab后面的字符不会算进来。

>>> name = 'kebi\t'
>>> name.expandtabs()
'kebi    '
>>> len(name.expandtabs())
8

>>> Like = 'Plane alone\t'   #超过8,那就补长到16
>>> Like.expandtabs()
'Plane alone     '
>>> len(Like.expandtabs())
16

>>> job = 'Have nothing to do\t'
>>> job.expandtabs()
'Have nothing to do      '
>>> len(job.expandtabs())
24


>>> name = 'kebi\tXGG'   #和tab键后面的字符没有关系
>>> name.expandtabs()
'kebi    XGG'
>>> len(name.expandtabs())
11

 15.strip():移除字符串首位指定的字符,默认删除空格。

   lstrip():移除左侧的指定字符

   rstrip():移除右侧的指定字符

>>> stt
'   kebi   '    #默认删除两侧的空格
>>> stt.strip()
'kebi'
>>> stt2 = '  nimei kebi'   #默认删除两侧的空格
>>> stt2.strip()
'nimei kebi'

>>> stt4 = 'qiuhjnqioq'
>>> stt4.strip('aoq')    #可以指定需要删除的字符,类似迭代
'iuhjnqi'
>>> stt4.strip('oaq')   #参数的顺序没有要求
'iuhjnqi'

>>> stt4.lstrip('oaq')    #lstrip()移除左边的指定字符
'iuhjnqioq'
>>> stt.lstrip()    
'kebi   '


>>> stt4.rstrip('oaq')    #rstrip()移除右边的指定字符
'qiuhjnqi'
>>> stt.rstrip()
'   kebi'

>>> sst = '  ke bi  '
>>> stt.strip()     #空格不会移除
'kebi'

16.format():字符串的格式化。

有三种方式:使用索引、使用位置参数和使用关键字。

format()格式化中,{}是格式化标识符,标记此处将会被格式化。

使用位置参数:把参数按照位置顺序来填充到字符串中

>>> fnc = 'name:{},age:{},sex:{}'
>>> fnc.format('kebi',25,'women')
'name:kebi,age:25,sex:women'

使用索引:

>>> fnc = 'name:{0},age:{1},sex:{2}'  #位置相对灵活
>>> fnc.format('kebi',25,'women')
'name:kebi,age:25,sex:women'
>>> fnc = 'name:{0},age:{2},sex:{1}'
>>> fnc.format('kebi','women',25)
'name:kebi,age:25,sex:women'

使用关键字:

>>> fnc = 'name:{a},age:{b},sex:{c}'
>>> fnc.format(a='kebi',c='women',b=25)
'name:kebi,age:25,sex:women'

还有不常见的一些属性,在对数字的格式化上面。

>>> "{:.2f}".format(3.1415926)  #保留两位小数
'3.14'
>>> "{:+.2f}".format(3.1415926)  #正数前面显示符号
'+3.14'
>>> "{:-.2f}".format(3.1415926)
'3.14'
>>> "{:.0f}".format(3.1415926)  #不带小数点
'3'
>>> "{:0>2d}".format(3)
'03'                          #前面用0填充
>>> "{:0>3d}".format(3)
'003'
>>> "{:0<3d}".format(3)   #后面用零填充
'300'
>>> "{:,}".format(123456789)
'123,456,789'    #用逗号分隔
>>> "{:.2%}".format(12)
'1200.00%'
>>> "{:.2e}".format(10000000)
'1.00e+07'   #表示成指数的形式
>>> "{:10d}".format(13)
'        13'            #向右对齐
>>> "{:>10d}".format(13)
'        13'
>>> "{:<10d}".format(13)  #向左对齐
'13        '
>>> "{:^10d}".format(13)  #中间对齐
'    13    '
>>> "{:b}".format(13)   #用二进制表示
'1101'
>>> "{:d}".format(13)   #用十进制表示
'13'
>>> "{:o}".format(13)    #用八进制表示
'15'
>>> "{:x}".format(13)     #用十六进制表示
'd'
>>> "{:#x}".format(13)
'0xd'
>>> "{:#X}".format(13)
'0XD'

 

相对与老版%格式化来说有3个有点:

(1).不用理会数据类型的问题

(2).单个参数可以多次输出

(3)填充方式十分灵活

17.isalnum():判断字符串是否由字母或数字组成,返回布尔值

   isalpha():判断字符串是否只由字母组成,返回布尔值

   isdigit():判断字符串是否只由数字组成,返回布尔值。

>>> 'dagev587'.isalnum()
True
>>> 'dagev 587'.isalnum()   #加入空格就不行了
False

>>> 'gogoing'.isalpha()    
True
>>> 'gogoing001'.isalpha()
False

>>> '001'.isdigit()
True
>>> 'gogoing001'.isdigit()
False

上面三个函数在实际中十分有用,因为返回的是布尔值,故而可以直接作为判定条件

posted @ 2017-10-24 19:15  明王不动心  阅读(2198)  评论(0编辑  收藏  举报