《Python基础教程》学习的第三课0121

今天学习python第三课,使用字符串。(所有标准的序列操作【索引、分片、乘法、判断成员资格、求长度、取最小与最大值】对字符串同样适用)

1.%(转换说明符)的用法,%标记了需要插入转换值的位置。

>>> formkk="hello ,%s. %s enough for you?"                                     //注释:使用%可以将字符串按顺序插入原始句子中。
>>> value=('world','egg')
>>> print formkk %value
hello ,world. egg enough for you?
>>>

  注意:若要输出%则需要使用%%。

 

2.格式化输出浮点数。(类比C的printf()输出函数)

>>> forma="Pi with three decimals:%.3f"                                                         //小数点后保留三位小数
>>> from math import pi
>>> print forma % pi
Pi with three decimals:3.142
>>>

 

3.简单介绍模板字符串

substitute 模板方法会用传递进来的关键字参数替换字符串中相应的$处。

>>> from string import Template
>>> s=Template('$x.glorious $x!')
>>> s.substitute(x='ssssss')
'ssssss.glorious ssssss!'
>>>

如果替换字是单词的一部分,那么参数名就必须用括号括起来

>>> from string import Template
>>> s=Template("it's ${x}tastic!")
>>> s.substitute(x='fan')
"it's fantastic!"
>>>

4.字符串方法

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

>>> 'with a moo-moo here, and a moo-moo there'.find('moo')
7
>>> title="money,money"
>>> title.find('money')
0
>>> title.find('m')
0
>>> title.find('n')
2
>>> title.find('t')
-1
>>>

还可以通过设置起始位置和终止位置来find

>>> sub='$$ get rich now! $'
>>> sub.find('$$')
0
>>> sub.find('$$',1,6)
-1
>>> sub.find('$$',0,6)
0
>>> sub.find('$',0,17)
0
>>>

join方法,是split方法的逆方法,用来连接字符串中的元素。

>>> b=['a','b','c']

>>> s='+'
>>> s.join(b)
'a+b+c'
>>> dirs='C:','usr','bin','eny'
>>> '/'.join(dirs)
'C:/usr/bin/eny'
>>> print 'D:'+'\\'.join(dirs)
D:C:\usr\bin\eny
>>>

 

lower方法,返回字符串中的小写字母版

>>> 'The older fisherman has a CAT'.lower()
'the older fisherman has a cat'

>>> name='RR'
>>> name2=['rr','DUBI']
>>> if name.lower() in name2:print 'found!'
found!
>>>

 

replace 方法返回某字符串的所有匹配项均被替换之后得到字符串

>>> 'The older fisherman has a CAT'.replace('older','younger')
'The younger fisherman has a CAT'
>>> 'The older fisherman has a CAT'.replace('s','88')
'The older fi88herman ha88 a CAT'
>>>

split 方法是join的逆方法,用来把字符串分割成序列

>>> '1+2+3+4'.split('+')
['1', '2', '3', '4']
>>> 'using the split method'.split()
['using', 'the', 'split', 'method']
>>>

translate 方法替换字符串中的某些部分,与replace不同的是,translate只处理单个字符。其优势在于可以同时进行多个替换,有时比replace效率高

>>> from string import maketrans
>>> table=maketrans('cs','kz')                                //把字符c替换为k,将s替换成z
>>> len(table)
256
>>> table[97:123]
'abkdefghijklmnopqrztuvwxyz'
>>> 'this is an incredible test'.translate(table)
'thiz iz an inkredible tezt'
>>> table=maketrans('s','8')
>>> 'this is an incredible test'.translate(table)
'thi8 i8 an incredible te8t'
>>>

小结,在使用translate转换之前,需要先完成一张转换表,转换表中是以某字符替换某字符的对应关系。使用string模板里的maketrans函数就行。

 

strip方法返回去除两侧(不包括内部)空格的字符串

>>> '             i am a gril     '.strip()
'i am a gril'
>>> names=['gumby','dooou','yullo']
>>> name='gumb y'
>>> if name.strip() in names:print 'found!'

>>> n='gumby  '
>>> if n.strip() in names:print 'found!'

found!
>>>

也可以指定去除的字符(去除两侧的字符)其实以上方法在处理脏数据时是经常用到的。

>>> uu='%%%he%llo%'
>>> uu.strip('%')
'he%llo'
>>>

 

 

 

 

posted @ 2018-01-21 14:16  壬人任仁  阅读(241)  评论(0编辑  收藏  举报