# 使用'''长字符串''',中间的不需要使用反斜线进行转义
# 原字符串 r'字符串' 注意:最后不能为\\
# 格式化字符串里包括%,须使用%%代替
# 可以使用$$,插入美元符号
# '%字段宽.精度' % 元素,字段宽前补0,-,+,' '分别表示前面补0,左对齐,现实正负号,前面加空格
# 字符串方法:
- find(,起始,结束),join(),lower()---返回小写字母版
- title()---首字母大写
- replace(from,to)
- split()是join的逆方法 例:'1+2+3+4'.split('+')
- strip(可以有参数)---去除两侧的空格
{{{
#字符串格式化
>>> format = 'Hello %s %s is enough for you?'
>>> x = ('hot','win')
>>> print(format % x)
Hello hot win is enough for you?
#字符串方法
>>> sep = '+'
>>> seq = list('guoyunlei')
>>> seq
['g', 'u', 'o', 'y', 'u', 'n', 'l', 'e', 'i']
>>> sep.join(seq)
'g+u+o+y+u+n+l+e+i'
>>> 'my name is guyunlei.'.title()
'My Name Is Guyunlei.'
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> a = 'helloworld!'
>>> t = a.maketrans('l','a')
>>> a.translate(t)
'heaaoworad!'
}}}
===模版字符串===
{{{
>>> s = Template('$x,hello,$x')
>>> s.substitute(x='guo')
'guo,hello,guo'
#使用字典进行替换
>>> s = Template('A $thing must never $action.')
>>> d = {
'thing':'gentleman',
'action':'show his socks'
}
>>> s.substitute(d)
'A gentleman must never show his socks.'
#使用*作为字段宽或精度,值从元组中读出
>>> '%.*s' % (5,'Guido ban Rossum')
'Guido'
}}}
==列表和元组==
# 列表:['guoyunlei',20]
# 所有的序列类型支持:indexing,sliceing,adding,multiplying以及检查是否属于该序列的成员
{{{
>>> four=input("Input the year:")[3]
Input the year:2012
>>> four
'2'
#输出最后三个数
>>> number = [1,2,3,4,5,6,7,8,9,0]
>>> number[7:10]
[8,9, 0]
>>> number[-3:]
[8, 9, 0]
>>> number[-3:-1]
[8, 9]
>>> number[-3:0]
}}}
所有元素 number[:]
number[8:3:-1] 步长为负会从右向左提取元素,
只有相同的序列才能相加。
整除 7//2 输出3
len(),max(),min().
{{{
>>>list('hello')
['h','e','l','l','o']
}}}
===删除元素===
del number[2]
===分片赋值===
{{{
>>> name = list('perl')
>>> name
['p', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['p', 'e', 'a', 'r']
#通过分片赋值删除元素
numbers = [1,2,3,4,5]
numbers[1:4] = []
>>>numbers
[1,5]
}}}
===列表方法===
对象.方法(参数)
# append 列表末尾追加
# count 某个元素在列表中出现的次数
# extend 在末尾一次性追加另一个序列中的更多的值
# index 从列表中找出某个值的第一个匹配的索引位置
# insert(位置,元素) 将方法用于将插入到列表中
# pop(参数) 删除列表中的一个元素,返回原位置的值
# remove(元素) 移除列表中的第一个匹配项
# reverse 元素反向存放
# sort 排序,另一种
获取已排序列表副本的方法,sorted,x=[1,2],y=sorted(x)
# sort(key= ,reverse= ),或者sort(函数)
==元组==
(1,2,3) (1,)
tuple(),把一个元素转换为元组
==字典==
===函数===
*dict* ---通过其它映射建立字典
{{{
items = [('name','hanbo'),('age',20)]
d = dict(items)
print(d)
f = dict(name='hanbo',age=20)
print(f)
}}}
===基本字典操作===
* items['key'] = valu会自动将不再字典的值添加到字典中
{{{
#使用字典格式化字符串
>>> phonebook = {
'Beth':'9120',
'Alice':'124',
'Cecil':'486'
}
>>> "Cecil's phone number is %(Cecil)s." % phonebook
"Cecil's phone number is 486."
}}}
====字典方法====
# clear无返回值
# copy是一种shallow copy,副本修改不影响原始字典,但修改会影响。
# deepcopy from copy import deepcopy
# fromkeys 给定的键建立新的字典,每个键默认对应的值为None。
# get 更宽松的访问方式。
# items 以列表形式返回,python3不同,注意。。。。。。
# keys 以列表形式返回key.
# pop 根据给的键值,删除字典中的键值对
# popitem 随机弹出字典中的键值对。
# setdefault 获取值,如果没有加入,有不会修改原值
# update 更新字典,相同的替换,不同的添加
# values 以列表形式返回字典中值
{{{
#copy()方法
>>> x = {'username':'admin','machines':['foo','bar','baz']}
>>> y = x.copy()
>>> y['username'] = 'hanbo'
>>> y['machines'].remove('baz')
>>> y
{'machines': ['foo', 'bar'], 'username': 'hanbo'}
>>> x
{'machines': ['foo', 'bar'], 'username': 'admin'}
#deepcopy
>>> from copy import deepcopy
>>> d = {}
>>> d['name'] = ['Alice','Bob']
>>> c = deepcopy(d)
>>> d['name'].append('Cecil')
>>> c
{'name': ['Alice', 'Bob']}
>>> d
{'name': ['Alice', 'Bob', 'Cecil']}
>>> {}.fromkeys(['name','age'])
{'age': None, 'name': None}
>>>d = {'name':'cat'}
x = d.setdefault('name')
>>>x
cat
}}}