标签: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: $ 42 Price of eggs in HEX: $2a |
1
2
3
4
5
6
7
|
from string import Templates s=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 59 True - 1 - 1 |
join & split, 连接和分割字符串
1
2
3
4
5
6
7
8
9
10
|
from macpath import join s=[ ‘ ‘ , ‘root‘ , ‘home‘ ] print ‘//‘ .join(s) s1= ‘C:‘ + ‘\\‘ .join(s) print s1 print s1.split( ‘\\‘ ) //root//home C: \root\home [ ‘C: ‘ , ‘root‘ , ‘home‘ ] |
1
2
3
4
5
|
s1 = ‘C:\root\home‘ print s1 C: 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 maketrans table = maketrans( ‘cs‘ , ‘kz‘ ) #建立一张替换规则表 print len(table) print ‘this is a magnificent day!‘ .translate(table, ‘!‘ ) #第二个参数用来指定要删除的字符 256 thiz iz a magnifikent day |
标签:style blog http io color ar os 使用 sp
原文地址:http://blog.csdn.net/dyllove98/article/details/41176683