3.1基本字符串操作
所有标准的序列操作对字符串同样适用
3.2字符串格式化:精简版
x="a %s ,%s,ada" #%指转换值位置,s转换值格式
y=("A","B")
print x%y
x="a %.4f ,ada" #%指转换值位置,.4f表示四位浮点数
from math import pi
print x%pi
输出结果
a A ,B,ada
a 3.1416 ,ada
Press any key to continue . . .
3.3字符串格式化:完整版
太多,待补
3.4字符串方法
#1.find在较长字符串中查找字串位置
print "hello world abcdefg".find("world")
x="hello world abcdefg"
print x.find("ab")
print x.find("1") #不存在则返回-1
#2.join split的逆方法,连接序列中的元素
x=["1","2","3"]
y="+"
print y.join(x)
#3.lower返回小写
print "ADS ASD asdasd".lower()
#4.replace返回某字符串的所有匹配项均被替换后得到的字符串
print "abcdefg".replace("bcd","123")
#5.split 拆分
print "1*2*3*4".split("*")
#6.strip 去除两侧
print " asdd ".strip()
#7.translate 替换单个字符 但可以替换多个
#创建词表
输出结果
6
12
-1
1+2+3
ads asd asdasd
a123efg
['1', '2', '3', '4']
asdd
Press any key to continue . . .