第一章 快速上手:基础知识
第二章 列表和元组
- 列表元素拼接:
''.join(lst)
- 列表方法
# 增:
lst.append(a) # 末尾增
lst.insert(3, 'n') # 插入
# 删
lst.clear() # 清空
lst.pop() # 末尾删
lst.remove('a') # 删除第一个指定元素
# 复制
b = lst.copy()
# 数数
lst.count(n)
# 扩展 (用来代替a=a+b)
a.extend(b)
# 索引
a.index('n')
# 排序
x.reverse() # 反向排列数据
x.sort() # 正向排列数据
第三章 字符串
1、字符串格式
# 单个值
>>> "i am a %s" % 'boy'
i am a boy
# 元组
>>> "i am a %s %s" % ('little', 'boy')
i am a little boy
>>> "i am a {0} {1}".format('little', 'boy')
i am a little boy
>>> "i am a {a} {b}".format(a="little", b="boy")
i am a little boy
# 字典
>>> phonebook
{'Beth':'9102', 'Alice':'2341'}
>>> "Beth's phone number is {Beth}".format_map(phonebook)
Beth's phone number is 9102
2、精度、宽度
# 单个值 精度
>>> "the number is %.2f" % 1
the number is 0.02
# 元组 精度+宽度
>>> "the number is {:10.2f}".format(2)
'the number is 2.00'
3、字符串方法
# join 合并
# 将列表合并为字符串
>>> '+'.join(['a','b'])
'a+b'
# replace 替换
>>> 'This is a test'.replace('This', 'That')
'That is a test'
# split 拆分
# 将字符串拆成列表
>>> '1+2+3+4'.split('+')
['1', '2', '3', '4']
# strip 删除首尾空格
# 默认为空格,还可以设置参数
>>>' i am a boy '.strip()
'i am a boy'
>>> '**! i am a boy!!** '.strip('*!')
' i am a boy!!** '