1.None 空
2.int 整型
3.bool 布尔类型
4.str 字符串
-
.upper() 转变成大写
a = 'adejkFa'
b = a.upper()
print(b) # ADEJKFA -
.lower() 转变成小写
a = 'SDADFDjkFa'
b = a.lower()
print(b) # 'sdadfdjkfa' -
.isdigit() 判断是否能转换成数字
content = input('请输入内容')
if content.isdigit():
print('转换成功')
else:
print('转换失败') -
.replace() 替换
a = 'ab-dsad-dfs-fdd-hg'
b = a.replace('-','|')
print(b) # 'ab|dsad|dfs|fdd|hg' -
.strip()/.rstrip()/.lstrip() 去除空白/去除右边空白/去除左边空白
v = ' 我是中国人 '
v1 = v.strip()
print(v1) # '我是中国人' -
.split()/.rsplit() 切割/从右边开始切割几个
v = 'adsnaaadnfdffnffnff'
v1 = v.split('n')
print(v1) # ['ads', 'aaad', 'fdff', 'ff', 'ff']
v2 = v.rsplit('n',2)
print(v2) # ['adsnaaadnfdff', 'ff', 'ff'] -
.startswith() 判断是否以()开头
a = 'alexodbly'
if a.startswith('al'):
print('是以‘al’开头')
else:
print('不是以‘al’开头') -
.endswith() 判断是否以()结尾
a = 'alexodboy'
if a.endswith('boy'):
print('是以‘boy’结尾')
else:
print('不是以‘boy’结尾') -
.format() 占位符
template = "我叫{0},年龄{1}岁,爱好{2}.".format('oldboy',55,'music')
print(template) #"我叫oldboy,年龄55岁,爱好music." -
.encode() 转换存储格式
name = "中国" # 默认使用unicode编码存储
v1 = name.encode('utf-8')
print(v1) -
"符号".join() 用什么符号连接
a = 'hdjhd'
a1 = '_'.join(a)
print(a1) # 'h_d_j_h_d'
-
5.list 列表
-
.append() 追加
name = ['马云','马化腾','李嘉诚']
name.append('王健林')
print(name) # ['马云','马化腾','李嘉诚','王健林'] -
.insert() 插入
name = ['马云','马化腾','李嘉诚']
name.insert(1,'王健林')
print(name) # ['马云','王健林','马化腾','李嘉诚'] -
.remove() 删除
name = ['马云','王健林','马化腾','李嘉诚']
name.remove('王健林')
print(name) # ['马云','马化腾','李嘉诚'] -
.pop() 删除
name = ['马云','王健林','马化腾','李嘉诚']
name.pop(2)
print(name) # ['马云','王健林','李嘉诚'] -
.clear() 清除
name = ['马云','王健林','马化腾','李嘉诚']
name.clear()
print(name) # [] -
.extend() 循环添加
name = ['马云','王健林','马化腾']
a = 'alex'
name.extend(a)
print(name) # ['马云', '王健林', '马化腾', 'a', 'l', 'e', 'x'] -
.reverse() 反转
val = [1,2,3,4,5,6,7,8]
val.reverse()
print(val) # [8, 7, 6, 5, 4, 3, 2, 1] -
.sort() 排列(False->从小到大排列,True->从大到小排列)
val = [1,54,24,6,3,27,14]
val.sort(reverse = False) #默认的,val.sort()是从小到大排列
print(val) # [1, 3, 6, 14, 24, 27, 54]
val.sort(reverse = True)
print(val) # [54, 27, 24, 14, 6, 3, 1]
-
6.tuple 元组
7.dict 字典
-
.keys() 获取所有的键
info = {'name':'oldboy','age':73,'hobby':'music'}
a = info.keys()
print(a) # ['name', 'age', 'hobby'] -
.values() 获取所有的值
info = {'name':'oldboy','age':73,'hobby':'music'}
a = info.values()
print(a) # ['oldboy',73,'music'] -
.items() 获取所有的键值对
info = {'name':'oldboy','age':73,'hobby':'music'}
a = info.items()
print(a) # [('name', 'oldboy'), ('age', 73), ('hobby', 'music')] -
.get() 判断索引是否存在
info = {'name':'oldboy','age':73,'hobby':'music'}
v = info.get('age')
if v == None:
print("'age'不存在")
else:
print(v) -
.pop() 删除
info = {'name':'oldboy','age':73,'hobby':'music'}
result = info.pop('age')
print(info,result) # {'name': 'oldboy', 'hobby': 'music'} 73 -
.update() 无则添加,有则更新。
info = {'name': 'oldboy', 'hobby': 'music'}
info.update({'age':73,'hobby': 'read'})
print(info)
-
8.set 集合
-
.add() 添加
v = {1,2,5,6,3,22}
v.add('oldboy')
print(v) # {1,2,5,6,3,22,'oldboy'} -
.discard() 删除
v = {1,2,5,6,3,22,'oldboy'}
v.discard(22)
print(v) # {1,2,5,6,3,'oldboy'} -
.update() 批量添加
v = {1,2,5,6}
v.update({'alex',22,'oldboy'})
print(v) # {1,2,5,6,'alex',22,'oldboy'} -
.intersection() 交集
v = {1,2,'alex',55,"中国"}
result = v.intersection({1,55,"中国",4})
print(result) # {1,55,"中国"} -
.union() 并集
v = {1,2,'alex',55,"中国"}
result = v.union({1,55,"中国",4})
print(result) # {1,2,'alex',55,"中国",4} -
.difference() 差集
v = {1,2,'alex',55,"中国"}
result = v.difference({1,55,"中国",4})
print(result) # {2,'alex'} -
symmetric_difference() 对称差集
v = {1,2,'alex',55,"中国"}
result = v.symmetric_difference({1,55,"中国",4})
print(result) # {2,'alex',4}
-