第三章 字符串
摘要: %s , $x , find() , join() , split() , lower() , title() , strip()
1、字符串格式化
format = "Hello,%s!Do you like %s ?"
name = ('Jack','Rose')
print format % name
%为转换说明符,标记需要插入转换值的位置。
%.3f 如果转化为浮点数,需要标记精度
使用string的template方法配合$格式化符号使用,替换字符串中相应位置的字符,如果是单词的一部分需要加{ }。
from string import Template
sep = Template('$x, do you like ${x}8?')
print sep.substitute(x = 'apple')
2、字符串方法
2、 hero = ['arcer','launcher','assassin']
print ' & '.join(hero) 在字符串序列中插入连接字符
3、name = 'Jack'
if name.lower() in ['jack','kangkang','jane']:print 'found!' 将字符串转化为小写
if name.title() in ['jack','kangkang','jane']:print 'found!' 将字符串转化为大写
4、print 'Jack is a good person'.replace('Jack','Joy') 查找替换
5、'arcer & launcher & assassin'.split('&') 分割
6、print ' I do not like working! '.strip() 去除两侧空格,也可以加参数去除两侧的字符