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

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 . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值