标签:style blog http io color ar os 使用 sp
|
1
2
3
4
5
|
left = "Hello,%s good " # %s 表示期望被格式化的类型right = "she‘s"print left % right # %用来隔开格式化字符串和待格式化值 Hello,she‘s good |
|
1
2
3
4
|
print "Price of eggs: $%d" %42 print "Price of eggs in HEX: $%x" %42 Price of eggs: $42Price of eggs in HEX: $2a |
|
1
2
3
4
5
6
7
|
from string import Templatess=Template("$x loves some one")print (s.substitute(x=‘she‘))print s she loves some one<string.Template object at 0x105bc1350> |
find ,等同于 in
|
1
2
3
4
5
6
7
8
|
s="the best movie"print s.find(‘movie‘)print ‘movie‘ in s print s.find(‘movie‘,10) #提供起始点,从index 10开始找print s.find(‘movie‘,1,5) #提供起始点和结束点,从index 1找到index 59True-1-1 |
join & split, 连接和分割字符串
|
1
2
3
4
5
6
7
8
9
10
|
from macpath import joins=[‘ ‘,‘root‘,‘home‘]print ‘//‘.join(s)s1= ‘C:‘ + ‘\\‘.join(s)print s1 print s1.split(‘\\‘) //root//homeC: \root\home[‘C: ‘, ‘root‘, ‘home‘] |
|
1
2
3
4
5
|
s1 = ‘C:\root\home‘print s1C:oot\home |
|
1
2
3
|
s=[‘ ‘,r‘root‘,‘home‘]print ‘C:‘+‘/‘.join(s)C: /root/home |
strip,去除字符串两侧的字符 (默认为空格)
translate, 同replace,但可以同时进行多个替换,效率更高。
|
1
2
3
4
5
6
7
8
9
10
|
from string import maketranstable = maketrans(‘cs‘,‘kz‘) #建立一张替换规则表print len(table) print ‘this is a magnificent day!‘.translate(table,‘!‘) #第二个参数用来指定要删除的字符256thiz iz a magnifikent day |
标签:style blog http io color ar os 使用 sp
原文地址:http://blog.csdn.net/dyllove98/article/details/41176683