字符串操作
in not in upper() isupper() lowser() islower() title() istitle()
isalpha() isalnum() isdecimal() isspace()
join() split() startswith endswith
rjust() ljust() center() strip() rstrip() lstrip()
快速构建一个字典序列
dict(zip('abcd',range(4))) # {'a': 0, 'b': 1, 'c': 2, 'd': 3}
3目运算
res = 'ok' if a==1 else 'ko'
推导列表生成字典
options={'code':'utf-8'}
base_headers={
'User-agent':100,
'Accept-Language':'zh-CN,zh,q=0.8'
}
"<< dict会生成新的字典 >>"
headers = dict(base_headers,**options)
print(headers)
# {'User-agent': 100, 'Accept-Language': 'zh-CN,zh,q=0.8', 'code': 'utf-8'}
"<< 或者使用update、更新原来的字典 >>"
base_headers.update(options)
print(base_headers)
# {'User-agent': 100, 'Accept-Language': 'zh-CN,zh,q=0.8', 'code': 'utf-8'}
"<< 使用update、也可以更新字典的值😯 >>"
options={'code':'UTF-8'}
base_headers.update(options)
print(base_headers)
# {'User-agent': 100, 'Accept-Language': 'zh-CN,zh,q=0.8', 'code': 'UTF-8'}
网友评论