首页 > 编程 > Python > 正文

第三章-使用字符串——python基础教程(第二版)笔记

2019-11-08 01:45:22
字体:
来源:转载
供稿:网友

3.1基本字符串操作

所有标准的序列操作对字符串同样适用

3.2字符串格式化:精简版

x="a %s ,%s,ada" #%指转换值位置,s转换值格式y=("A","B")PRint x%yx="a %.4f ,ada" #%指转换值位置,.4f表示四位浮点数from math import piprint x%pi

输出结果

a A ,B,adaa 3.1416 ,adaPress 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 替换单个字符 但可以替换多个#创建词表

输出结果

612-11+2+3ads asd asdasda123efg['1', '2', '3', '4']asddPress any key to continue . . .
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表