一.str字符串
1.capitalize字符串首字母大写
name = "json" v = name.capitalize() print(v) # 输出结果:
Json
2.casefold将所有大写变小写
name = "JsoN" v = name.casefold() print(v) # 输出结果: json
3. lower将所有大小写变小写
name = "JsoN" v = name.lower() print(v) # 输出结果 json
4.center文本居中
# 参数1:表示总长度 # 参数2:空白处填充的字符(长度为1) name = "json" v1 = name.center(20) print(v1) # 输出结果 json v2 = name.center(20, "*") print(v2) # 输出结果 ********json********
5.count表示传入值在字符串中出现的次数
# 参数1: 要查找的值(子序列) # 参数2: 起始位置(索引) # 参数3: 结束位置(索引) name = "asjijoiashashhhashhol" v = name.count("h") # 查找h出现的次数 print(v) v = name.count("as") # 查找as出现的次数 print(v) v = name.count("as", 12) # 查找索引12之后的所有as出现的次数 print(v) v = name.count("as", 0, 12) # 查找索引0-12之间as出现的次数 print(v)
6.endwish是否以"on"结尾,是返回True,否返回False
name = "json" v = name.endswith("on") print(v) # 返回结果 True
7.startswith是否以"js"开头,是返回True,否返回False
name = "json" v = name.startswith("js") print(v) # 返回结果 True
8.expandtabs找到制表符 ,进行替换默认是空格(包括前边的值)
name = "js s n json uu kkk" v = name.expandtabs(20) print(v) # 输出结果 js s n json uu kkk
9.find找到指定子序列的索引位置:存在返回索引,不存在返回-1
name = "json" v = name.find("0") print(v) # 输出结果 2 v = name.index("o") print(v) # 输出结果 -1
10.字符串格式化
tal = "我是:%s;年龄:%s;性别:%s" # 方法一 tpl = "我是:{0};年龄:{1};性别:{2}" v = tpl.format("json", 20, "男") print(v) # 方法二 tpl = "我是:{name};年龄:{age};性别:{gender}" v = tpl.format(name="json", age=20, gender="男") print(v) # 方法三 tpl = "我是:{name};年龄:{age};性别:{gender}" v = tpl.format_map({"name":"json", "age":"20", "gender": "男"}) print(v) # 输出结果 我是:json;年龄:20;性别:男
11.isalnum判断是否包含数字,汉字。isalpha是否只是字母组成。是返回True,否返回False
name = "json88汉字" v = name.isalnum() print(v) #返回结果 True v2 = name.isalpha() print(v2) #返回结果 False
12.判断是否是数字,是返回True,否返回False
num = "123②二" v1 = num.isdecimal() v2 = num.isdigit() # 可以识别"123","②" v3 = num.isnumeric() # 可以识别"123","②","二" print(v1, v2, v3)
13.isidentifier是否是表示符
n = "name" v = n.isidentifier() print(v) # 返回结果 True
14.判断是否是小写和大写
name = "JSON" v1 = name.islower() # 返回结果 False v2 = name.isupper() # 返回结果 True print(v1, v2)
15.upper全部变为大写
name = "Josn" v = name.upper() print(v) # 输出结果 JSON
16.isprintable是否包含可打印字符
name = "钓鱼就要钓刀鱼, 刀鱼就要岛上钓 " v = name.isprintable() print(v) # 返回结果 False
17.isspace判断是否全部是空格
name = " " v = name.isspace() print(v) # 返回结果True
18.元素拼接
name = "json" v = "_".join(name) print(v) # 输出结果 j_s_o_n # 列表中的元素进行拼接 name_list = ["小明", "小红", "小兰"] v = "和".join(name_list) print(v) #输出结果 小明和小红和小兰
19.填充
name = "json" v1 = name.center(20, "*") # 居中填充 v2 = name.rjust(20, "*") #右填充 v3 = name.ljust(20, "*") #左填充 print(v1, v2, v3)
20.对应关系+翻译
m = str.maketrans("json", "1234") #对应关系 name = "jdahdhudqwdgyqwgdyqwjson" v = name.translate(m) #将对应关系翻译出来 print(v) # 输出结果 1dahdhudqwdgyqwgdyqw1234
21.分割保留分割元素
name = "小明and小红and小花" v = name.replace("and", "love") # 替换全部 print(v) 输出结果 小明love小红love小花 v2 = name.replace("and", "love", 1) #只替换索引1 print(v2) 输出结果 小明love小红and小花
22.strip移除空白, , 自定义
name = "json " v = name.strip() # 白, , print(v) # 输出结果 json
23.swapcase大小写转换
name = "Json" v = name.swapcase() # 把大写转为小写,把小写转为大写 print(v) # 输出结果 jSON
24.填充
name = "json" v = name.zfill(20) #填充20个0 print(v) # 输出结果 0000000000000000json
25.索引
name = "json" name[0] name[0:3] name[0:3:2] # 从0-3间隔是2
26.转换成字节
name = "李永乐" v1 = name.encode(encoding = 'utf-8') print(v1) #输出结果 b'xe6x9dx8exe6xb0xb8xe4xb9x90' v2 = name.encode(encoding = 'gbk') print(v2) #输出结果 b'xc0xeexd3xc0xc0xd6'
二.int整数类型
1.当前整数的二进制表示,最少位数
age = 4 print(age.bit_length())
2.获取当前数据的字节表示
age = 18 v1 = age.to_bytes(10, byteorder="big") v2 = age.to_bytes(10,byteorder="little") print(v1, v2)
三.bool布尔值
1.正确返回True
2.错误返回False
四.列表
1.append追加
user_list = ["小明", "小红", "小兰", "小花"] user_list.append("小龙") print(user_list)
2.clear清空列表
user_list = ["小明", "小红", "小兰", "小花"] user_list.clear() print(user_list)
3.copy浅拷贝
user_list = ["小明", "小红", "小兰", "小花"] user_list.clear() print(user_list)
4.count计数
user_list = ["小明", "小红", "小兰", "小花", "小花"] v = user_list.count("小花") print(v)
5.extend扩展原表
user_list = ["小明", "小红", "小兰", "小花", "小花"] user_list.extend(["周杰伦", "黄晓明"]) print(user_list) #输出结果:['小明', '小红', '小兰', '小花', '小花', '周杰伦', '黄晓明']
6.index查找元素索引,没有报错
user_list = ["小明", "小红", "小兰", "小花", "小花"] v = user_list.index("小明") print(v)
7.pop删除并获取元素
user_list = ["小明", "小红", "小兰", "小花", "小花"] v = user_list.pop(1) print(v) # 输出结果:小红 print(user_list) # 输出结果:['小明', '小兰', '小花', '小花']
8.remove删除值
user_list = ["小明", "小红", "小兰", "小花", "小花"] user_list.remove("小红") print(user_list) #输出结果:['小明', '小兰', '小花', '小花']
9.reverse翻转
user_list = ["小明", "小红", "小兰", "小花"] user_list.reverse() print(user_list) # 输出结果:['小花', '小兰', '小红', '小明']
10.排序
number = [22, 2, 33, 4, 0] number.sort() # 从小到大 print(number) number.sort(reverse=True) # 从大到小 print(number)
11.索引
user_list = ['李泉', '刘一', '李泉', '刘康', '豆豆', '小龙'] # 根据索引取值 print(user_list[0]) # 李泉 print(user_list[1:5:2]) # ['刘一', '刘康'] # 根据索引删除列表元素 del user_list[3]
五.range,enumrate的应用
range的应用
1.range在python2x与3x的区别
2.7: 立即生成所有数字
3.x: 不会立即生成,只有循环迭代时,才一个一个生成
2.range: 三个参数
li = ['eric', 'alex', 'tony'] # range,len,li循环 for i in range(0, len(li)): ele = li[i] print(ele)
enumerate的应用
1.额外生成一列有序的数字
# /user/bin/python # -*- coding:utf-8 -*- li = ['eric', 'alex', 'tony'] for i, ele in enumerate(li, 1): print(i, ele) ''' 输出结果是: 1 eric 2 alex 3 tony '''
六.元组
1.获取个数
# /user/bin/python # -*- coding:utf-8 -*- user_tuple = ('alex', 'eric', 'seven', 'alex') v = user_tuple.count('alex') print(v) ''' 输出结果是: 2 '''
2.获取值的第一个索引位置
# /user/bin/python # -*- coding:utf-8 -*- user_tuple = ('alex', 'eric', 'seven', 'alex') v = user_tuple.index('alex') print(v) ''' 输出结果是: 0 '''
3.遍历元组
# /user/bin/python # -*- coding:utf-8 -*- user_tuple = ('alex', 'eric', 'seven', 'alex') for i in user_tuple: print(i) ''' 输出结果是: alex eric seven alex '''
4.元组嵌套列表
# /user/bin/python # -*- coding:utf-8 -*- user_tuple = ('alex', 'eric', 'seven', ['陈涛', '刘浩', '赵芬芬'], 'alex') user_tuple[3][1] = '刘一' print(user_tuple) ''' 输出结果是: ('alex', 'eric', 'seven', ['陈涛', '刘一', '赵芬芬'], 'alex') '''
5.元组最后,加逗号
li = ('alex',)
七.字典
1. 清空
dic = {'k1':'v1','k2':'v2'} dic.clear() print(dic) ''' 输出结果是: {} '''
2.浅拷贝
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} v = dic.copy() print(v) ''' 输出结果是: {'k1': 'v1', 'k2': 'v2'} '''
3.根据key获取指定的value
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} v = dic.get('k1') print(v) v = dic['k2'] print(v) ''' 输出结果是: v1 v2 '''
4.删除并获取对应的value值
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} v = dic.pop('k1') print(dic) print(v) ''' 输出结果是: {'k2': 'v2'} v1 '''
5.随机删除键值对,并获取到删除的键值
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} v = dic.popitem() print(dic) print(v) ''' 输出结果是: {'k2': 'v2'} ('k1', 'v1') '''
6.增加,如果存在则不做操作
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} dic.setdefault('k3', 'v3') print(dic) dic.setdefault('k1', '1111111') print(dic) ''' 输出结果是: {'k2': 'v2', 'k3': 'v3', 'k1': 'v1'} {'k2': 'v2', 'k3': 'v3', 'k1': 'v1'} '''
7.批量增加或修改
# /user/bin/python # -*- coding:utf-8 -*- dic = {'k1': 'v1', 'k2': 'v2'} dic.update({'k3': 'v3', 'k1': 'v24'}) print(dic) ''' 输出结果是: {'k2': 'v2', 'k1': 'v24', 'k3': 'v3'} '''
8.字典可以嵌套,但是key: 必须是不可变类型
# /user/bin/python # -*- coding:utf-8 -*- dic = { 'k1': 'v1', 'k2': [1, 2, 3,], (1, 2): 'lllll', 1: 'fffffffff', 111: 'asdf', } print(dic) ''' 输出结果是: {'k1': 'v1', 'k2': [1, 2, 3], (1, 2): 'lllll', 111: 'asdf', 1: 'fffffffff'} '''
八.集合,不可重复的列表
1.s1中存在,s2中不存在
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} v = s1.difference(s2) print(v) # s1中存在,s2中不存在,然后对s1清空,然后在重新复制 s1.difference_update(s2) print(s1) ''' 输出结果是: {'李泉', '李泉11'} {'李泉', '李泉11'} '''
2.s2中存在,s1中不存在
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} v = s2.difference(s1) print(v) ''' 输出结果是: {'刘一'} '''
3.s1中存在,s2中不存在
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} v = s1.difference(s2) print(v) ''' 输出结果是: {'李泉11', '李泉'} '''
4.交集
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} v = s1.intersection(s2) print(v) ''' 输出结果是: {'tony', 'alex', 'eric'} '''
5.并集
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} v = s1.union(s2) print(v) ''' 输出结果是: {'eric', '李泉', '刘一', 'alex', 'tony', '李泉11'} '''
6.移除
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} s2 = {"alex", 'eric', 'tony', '刘一'} s1.discard('alex') print(s1) ''' 输出结果是: {'eric', '李泉11', 'tony', '李泉'} '''
7.遍历
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11'} for i in s1: print(i) ''' 输出结果是: tony alex eric 李泉11 李泉 '''
# /user/bin/python # -*- coding:utf-8 -*- s1 = {"alex", 'eric', 'tony', '李泉', '李泉11', (11, 22, 33)} for i in s1: print(i) ''' 输出结果是: 李泉 (11, 22, 33) eric 李泉11 tony alex '''