字符串类型
split通过指定分隔符分割字符串
例子:
name = "huang zhong"
print(name.split())
strip移除字符串头部和尾部的字符,默认空格, lstrip移除左侧字符,rstrip移除右侧字符
例子:
name = " alex "
print(name.strip())
capitalize首字母转换成大写,字符串自身不变,生成新的字符串
例子:
name = "alex"
print(name.capitalize())
casefold将所有大写变小写,可以转换多门语言
例子:
name = "AleX"
v = name.casefold()
print(v)
lower将所有大写变小写,只能转换英语
例子:
name = "AleX"
v = name.casefold()
print(v)
center文本居中,空白处用其他字符填充,默认空白字符填充
例子:
name = "alex"
v = name.center(20, '*')
print(v)
count子字符串在父字符串中出现的次数
例子:
name = "cndklsdnrfknsv"
num = name.count("d")
num1 = name.count("d", 5) #从第5个索引开始计算
num2 = name.count("d", 5, 9) #计算从第5到第9之间d出现的次数
print(num)
startswith是否以某个字符开始,是则返回True,否则返回False
例子:
name = "alex"
v = name.startswith("al")
print(v)
endswith是否以某个字符开始,是则返回True,否则返回False
例子:
name = "alex"
v = name.startswith("ex")
print(v)
expandtabs找到制表符并替换
例子:
name = "nsdl\tajnfcks\tsns"
v = name.expandtabs(20) #替换成20个字符,不足的补空白
print(v)
find找到子序列的索引位置,没找到返回-1
例子:
name = "alex"
v = name.find("o") #-1
print(v)
index找到子序列的索引位置,没找到报错
例子:
name = "alex"
v = name.index("a")
print(v)
format字符串格式化
例子:
1.str = "我是{0}, 年龄{1}, 性别{2}"
v = str.format("alex", "19", "男")
print(v)
2. str = "我是{name}, 年龄{age}, 性别{sex}"
v = str.format(name="alex",age="19", sex="男")
print(v)
isalnum是否是数字、字符、汉字, 返回True或False
例子:
name = "alex8汉字"
v = name.isalnum()
print(v)
isdecimal,isdigit,isnumeric判断是否是数字
例子:
name = "二"
v1 = name.isdecimal() #是2才返回True
v2 = name.isdigit() #是2或者二都返回True
v3 = name.isnumeric() #是其他形式的2也返回True
isidentifier是否是表示符
例子:
name = "alex"
v = name.isidentifier()
print(v)
islower是否全部是小写
例子:
name = "ALEX"
v = name.islower()
print(v)
isupper是否全部是大写
例子:
name = "ALEX"
v = name.isupper()
print(v)
upper将所有小写转大写
例子:
name = "alex"
v = name.upper()
print(v)
isprintable()是否包含可打印字符串
例子:
name = "钓鱼要钓刀鱼, \n刀鱼要到岛上钓" #\n属于隐含不可打印字符串
v = name.isprintable()
print(v)
isspace是否全部是空格
例子:
name = " "
v = name.isspace()
print(v)
join元素拼接, 字符串列表都可以拼接
例子:
name = "alex"
v = "_".join(name) #用"_"把name的每个字符拼接起来
print(v)
center, rjust,ljust左右中间填充
例子:
name = "alex"
v1 = name.center(20, "*")
v2 = name.ljust(20, "*")
v3 = name.rjust(20, "*")
print(v1)
maketrans对应关系映射,translate翻译对应关系
例子:
m = str.maketrans('aeiou', '12345')
name = "cbdwlrcwjmcqieqxeqw"
v = name.translate(m)
partition分割字符串,保留分割符
例子:
content = "李泉and李冰and李响"
v = content.partition("and")
print(v)
replace替换字符串
例子:
content = "李泉and李冰and李响"
v = content.replace("and", "or")
v1 = content.replace("and", "or", 1) #替换1次
print(v)
swapcase大小写转换
例子:
name = "AleX"
v = name.swapcase()
print(v)
zfill填充
例子:
name = "alex"
v = name.zfill(20) #用0填充
print(v)
2.×××
bit_length整数所占的位数
例子:
age = 4
v = age.bit_length()
print(v)
to_bytes获取当前数据的字节表示
例子:
age = 15
v = age.to_bytes(10, byteorder="big")
v1 = age.to_bytes(10, byteorder="little")
print(v,v1)
3.bool数据
只有True, False两种
4.list列表
append追加元素
例子:
li = ["aa", "bb", "cc", "dd"]
li.append("ee")
print(li)
clear清空列表
例子:
li = ["aa", "bb", "cc", "dd"]
li.clear()
print(li)
copy浅拷贝
例子:
li = ["aa", "bb", "cc", "dd"]
v = li.copy()
print(v)
count计数
例子:
li = ["aa", "bb", "cc", "dd", "cc"]
v = li.count("cc")
print(v)
extend扩展原列表
例子:
li = ["aa", "bb", "cc", "dd"]
li1 = ["ee", "ff"]
li.extend(li1)
print(li)
index查找元素索引,没有报错
例子:
li = ["aa", "bb", "cc", "dd"]
v = li.index("bb")
print(v)
pop删除元素,并返回删除的元素
例子:
li = ["aa", "bb", "cc", "dd"]
v = li.pop(1)
print(v)
remove删除值
例子:
li = ["aa", "bb", "cc", "dd"]
li.remove("bb")
print(li)
reverse反转列表
例子:
li = ["aa", "bb", "cc", "dd"]
li.reverse()
print(li)
sort排序
例子:
li = ["aa", "bb", "cc", "dd"]
li.sort() #默认从小到大
li.sort(reverse=True) #从大到小
print(li)
切片
例子:
li = ["aa", "bb", "cc", "dd"]
li[1:3] #取第二到第三个元素
li[0:3:2] #取到aa, cc , 2是步长
range生成有序数列, python3版本不会一次创建所有数列,而是循环迭代一次一个数字创建
例子:
for i in range(1,11):
print(i)
for i in range(1,11,2): #步长是2
print(i)
enumerate额外生成一列有序数字
例子:
li = ["aa", "bb", "cc", "dd"]
for i, v in enumerate(li, 1): #初始数字从1开始,可以自己设置
print(i, v)
4.元组不可变类型
count获取元素出现在tuple中的次数
例子:
tu = ("aa", "bb", "cc", "dd","bb")
v = tu.count("bb")
print(v)
index获取值的第一个索引位置
例子:
tu = ("aa", "bb", "cc", "dd","bb")
v = tu.index("bb")
print(v)
5.dict字典
clear清空字典
例子:
dic = {"k1": "v1", "k2": "v2"}
dic.clear()
print(dic)
copy浅拷贝
例子:
dic = {"k1": "v1", "k2": "v2"}
v = dic.copy()
print(v)
get根据key获取值,不存在不报错
例子:
dic = {"k1": "v1", "k2": "v2"}
v = dic.get("k1", 111) #不存在默认设置成111
print(v)
pop删除并获取对应的value
例子:
dic = {"k1": "v1", "k2": "v2"}
v = dic.pop("k2")
print(dic)
print(v)
popitem随机删除键值对
例子:
dic = {"k1": "v1", "k2": "v2"}
v = dic.popitem()
print(dic, v)
setdefault增加键值对,如果存在则不操作
例子:
dic = {"k1": "v1", "k2": "v2"}
dic.setdefault("k3","v3")
print(dic)
update批量增加或修改
例子:
dic = {"k1": "v1", "k2": "v2"}
dic.update({"k3":"v3", "k4":"v4"})
print(dic)
fromkeys多键同时赋一个值
例子:
dic = dict.fromkeys(['k1','k2','k3'], 123)
print(dic)
append追加元素
例子:
dic = {"k1": "v1", "k2": "v2"}
dic['k3'].append("v3")
print(dic)
6.set集合
difference s1中存在,s2中不存在
例子:
s1 = {1,2,3,4}
s2 = {1,5,6,4}
s1.difference(s2)
print(s1)
difference_update s1中存在,s2中不存在然后对s1清空重新赋值
例子:
s1 = {1,2,3,4}
s2 = {1,5,6,4}
s1.diffenerce_update(s2)
print(s1)
symmetric_diffenerce s1中存在,s1中不存在,s2中存在,s1中不存在
例子:
s1 = {1,2,3,4}
s2 = {1,5,6,4}
v = s1.symmetric_diffenerce(s2)
print(v)
intersection交集
例子:
s1 = {1,2,3,4}
s2 = {1,5,6,4}
v = s1.intersection(s2)
union并集
例子:
s1 = {1,2,3,4}
s2 = {1,5,6,4}
v = s1.union(s2)
discard移除
例子:
s1 = {1,2,3,4}
s1.discard(2)
print(s1)
update更新
例子:
s1 = {1,2,3,4}
s1.update({1,2,5,6})
print(s1)