Day5_python(基础知识)
endswith("")已什么结尾
startwith()已什么开头
swapcase()大小写互换
title()首字母大写
zfillz(16) 十六进制补位
expandtabs(tabsize=30)/t转换为空格
find()查找(字符串也可以切片)
nameformat()
isalnum 阿拉伯数字
isalpha 包含大写
isdecimal 十进制数
isdigit 数字
isidentifier 判断是否是合格的变量名
isnumeric 判断是否只有数字
istitle 判断第一个是否为大写
isprintable tty
isupper 全部大写
join
print('+'.join(['1','2','3']))
1+2+3
ljust(50,*) 左边加*
rjust(50,*) 右边加*
lower 小写
upper 大写
strip 去掉两遍的空格和回车
lstrip
rstrip
p = str.maketrans("abcde",'12345')
print("hu yong".translate(p))
replace('l','L',1)
rfind('l') 查找到最后一个值的下标
split('h') 按照l为分隔符生成列表\n
splitlines() 按照换行来分
name = "my name is {name} and i am {year} old"
name.format(name='huyong',year=25)
name.format_map({'name':'alex','year':12})
字典:
info{
'student01'
}
info.pop("student01") #标准删除
del info['student01'] #第二种删除
info.popitem() #随机删除一种
info["student01"] #按照key去查找
print(info.get('student01')) #永远不会出错
print('student01' in info) #info.has_key("1103"),2.x版本
province[]
province.setdefault("taiwan",{"www.baidu.com":[1,2]})
info.update(b) #合并字典,相同的覆盖
info.items() #字典改变成列表
info.formkeys([6,7,8],"test") #初始化一个值,字典多层的时候,给每个key赋的值都
为同一个内存地址
for i in info: 循环效率高效
print(i,info[i])
for k,v in info.items(): 需要先转换为列表,不高效
print(k,v)
集合:
list_1 = [1,2,3,5,6,7]
list_1 = set(list_1)
print(list_1,type(list_1))
list_2 = set([2,6,8,9])
list_1.intersection(list_2) #取交集
list_1.union(list_2) #并集
list_1.difference(list_2) #差集
list_1.issubset(list_2) #判断子级
list_1.issuoerset(list_2) #判断子级
list_1.symmetric_difference(list_2) #对称差集
list_1.isdisjoint(list_2) #判断是否有交集
交集 intersection
print(list_1 & list_2)
并集 union
print(list_1 | list_2)
差集 difference
print(list_1 - list_2)
对称差集
print(list_1 ^ list_2)
add 添加一项
update 添加多项
remove 删除一项
discard 对比remove删除不会报错
文件操作:
data = open("huyong",encoding="utf-8").read()
print(data)
f=open("huyong",encoding="utf-8")
data=f.read()
f=open("yesterday",'r',encoding="utf-8")
f=open("yesterday",'w',encoding="utf-8")
f=open("yesterday",'a',encoding="utf-8")
f.write("写入,")
f.write("进去")
for index,line in enumerate(f.readlines()):
if index == 9:
print('-----我是分割线-----')
continue
print(line.strip())
for index,line in enuymerate(f.readlines()):
if index == 9:
print('我是分割线.center(40)')
continue
print(line.strip())
count = 0
for line in f: #一行行的打印
if count == 9:
print('我是分割线.center(40)')
count+=1
continue
print(line)
count+ = 1
计数
print(f.tell()) 打印当前位置
print(f.readline()) 打印一行
f.seek(10) 从当前第10个字符地方打印
f.isatty() #查看终端设备
seekable() #判断光标是否可以移动
readable() #判断文件是否可读
print(f.flush()) #实时刷新
import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
sleep.time(0.1)
f.truncate() #不写就是清空,写了就是从结尾清空到该位置
a+ #追加读写 可读可追加,追加写
r+ #读写 (重要)可读可追加,追加写
W+ #写读
rb #二进制文件 以二进制文件去读
encode
decode
f = open("yesterday2","r",encoding="utf-8")
f_new= open("yesterday2","w",encoding="utf-8")
for line in f:
if "小球是个好人" in line:
line = line.replace("小球是个好人","小球不是个好人")
f_new.write(line)
人的痛苦主要是源于自己无能的愤怒!