test = 'aLexaLex'
v = test.find('ex',5,7) #默认从开始往后找现在设置5到7的闭开区间print(v) #找到第一个之后,获取其位置,没找到返回-1
test = '你是风儿我是沙'print(test)
t = ' '
v = t.join(test) #将字符串中的每一个元素按照指定分隔符进行拼接print(v)
test = 'aLex'
v1 = test.islower() #判断全是小写
v3 = test.isupper() #判断全是大写
v2 = test.lower() #全是变成小写
v4= test.upper() #全是变成大写print(v1,v2,v3,v4)
test = 'testasdsdfgs'
v1 = test.rpartition('s') #找到最右边的字符分割成3份
v2 = test.partition('s') #找到最左边的字符分割成3份
v3 = test.rsplit('s',2) #找到字符(默认全部,这找2个从右边开始),去除字符并分割
v4 = test.split('s') #找到字符,去除字符并分割print(v1,v2,v3,v4)
test = '\n1 \naLe xx'
v1 = test.lstrip() #去除空白\n\t,从最左边开始连续的
v2 = test.rstrip() #去除空白\n\t,从最右边开始连续的
v3 = test.strip() #去除空白\n\t,从2边开始连续的
v4 = test.rstrip('x') #指定去除x,从2变开始连续的print(v1,v2,v3,v4)
test = 'aLex'
v = test[0:2]
v = len(test) #可以读列表元素
index = 0while index < len(test):
index += 1for index in test:
print(index)
v = range(0,100,5) #创建一个100个数的数组,for循环才真实创建,默认0开始,可设定0到100步长5foritemin rang(0,len(test)):
print(item,test[item])
print('=====================')
test = 'aLex'
v = test.capitalize() #首字母大写其他字母小写print(v)
test = 'aLex'
v1 = test.casefold() #将字符串中所有语言中大写变小写print(v1)
v2 = test.lower() #将字符串中英文大写变小写print(v2)
test = 'aLex'
v = test.center(20,'*') #设置宽度,并内容居中,20代指总长度,*空白未知填充,一个字符,可有可无
v1 = test.ljust(20,'*') #设置宽度,并内容居左,20代指总长度,*空白未知填充,一个字符,可有可无
v2 = test.rjust(20,'*') #设置宽度,并内容居右,20代指总长度,*空白未知填充,一个字符,可有可无
v3 = test.zfill(20) #设置宽度,并内容居右,20代指总长度,只用0来填充print(v,v1,v2,v3)
test = 'aLexaLex'
v = name.count('ex') #去字符串中寻找子序列出现次数print(v)
test = 'aLexaLex'
v = name.count('ex',6,8) #数字符出现次数,默认全部,设定从第6个到第8个的闭开区间print(v)
test = 'aLex'
v = test.endswith('ex') #判断以什么结尾print(v)
v = test.startwith('ex') #判断以什么开始print(v)
test = "username\temail\tpassword\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123"
v = test.expandtabs(20) #\t是制表符,expandtabs,断句20,到\t不足20补空格print(v,len(v))
test = 'i am {name},age {a}'print(test)
v = test.format(name='alex',a=19) #格式化,将一个字符串中的占位符替换为指定的值print(v)
test = 'i am {0},age {1}'print(test)
v = test.format('alex',19) #格式化,将一个字符串中的占位符替换为指定位置的值print(v)
test = 'i am {name},age {a}'
v = test.format_map({"name":'alex',"a":19}) #格式化,传入的值只能是字典print(v)
test = 'aLexaLex'
v = test.index('ex',5,8) #默认从开始往后找现在设置5到7的闭开区间print(v) #找到第一个之后,获取其位置,没找到报错
test = "uasf890"
v = test.isalnum() #判断字符串中字符串中是否只包含数字和字母print(v)
test = "uasf"
v = test.isalpha() #判断字符串中字符串中是否只包含字母print(v)
test = "二"
v1 = test.isdecimal() #判断字符串中字符串中是否只包含数字 2
v2 = test.isdigit() #判断字符串中字符串中是否只包含数字(还有数字符号)②
v3 = test.isnumeric() #判断字符串中字符串中是否只包含数字(还有数字符号、汉字)二print(v1,v2,v3)
a = "la_123"
v = a.isidentifier() #判断标志符 标识符只包含,字母、数字、下划线且首字母不是数字print(v)
test = 'oiue\turyyffb'
v = test.isprintable() #判断字符串是否存在不可显示的字符\t制表\n换行print(v)
test = ' '
v = test.isspace() #判断字符全空格print(v)
test = 'sdusdjnd and sdin is hwj'
v1 = test.istitle() #判断标题,也是所有单词首字母大写
test1 = test.title()
v2 = test1.istitle()
print(v1,test1,v2)
v = 'aesdioujkdfhiudoiasdoiuf'm = str.maketrans('aeiou','12345') #设定前后对应关系
new_v = v.translate(m) #对m中字符进行对应关系变换print(new_v)
v = 'alexalex'
new_v = v.replace('ex','apeiou',2) #对匹配到的序列替换,默认全替换,现在只替换前2个print(new_v)
test = 'asdfadsf\nasdasfdsf\nsdfa'
v = test.splitlines(True) #分割,只能根据,true,false;是否保留换行print(v)
test = 'backend1.1.1.1'
v = test.startswith('a') #判断以XXX开头
v1 = test.endswith('a') #判断以XXX结尾print(v,v1)
test = 'aLex1'
v = test.swapcase() #大小写转换print(v)
列表
li = [11, 22, 33, 44]
v = li.append(5) #直接修改list无返回值,元素做整体追加print(li)
li = [11, 22, 33, 44]
li.extend([22]) #把元素循环扩展到列表中print(li)
li = [11, 22, 33, 44]
li.clear() #清空print(li)
li = [11, 22, 33, 44]
v = li.copy() #浅拷贝print(v)
li = [11, 22, 33, 44]
v = li.count(22) #计算元素出现的个数print(v)
li = [11, 22, 33, 44]
v = li.index(22) #根据值获取当前值获取位置print(v)
li = [11, 22, 33, 22, 44]
li.insert(0, 99) #在指定索引位置插入元素print(li)
li = [11, 22, 33, 22, 44]
v = li.pop() #默认删除最后一个位置的值并返回当前值print(li,v)
li = [11, 22, 33, 22, 44]
li.remove(11) #删除第一个指定值print(li)
li = [11, 22, 33, 22, 44]
li.remove(11) #删除第一个指定值print(li)
li = [11, 22, 33, 22, 44]
li.reverse() #将当前列表反转print(li)
li = [11, 22, 33, 22, 44]
li.sort(reverse=True) #列表从小到大排序,现在从大到小print(li)
li = [1, 2, 43, [1, 2, 3], "age", "alex"]
for item in range(0, 5):
print(li[item])
print(li[3][2])
li[1:3] = [1, 20, 32, 33]
print(li)
del li[1:3]
元组
tu = (11, 22, 11, 33, 44,)
tu.count(11) # 获取指定元素在元组中出现次数
v = tu.index(11) # 获取指定元素在元组中第一个的位置
print(v)
tu = (111, 222, 333, ) # 元素不可被修改。
v = tu[0]
v = tu[0:2]
foritemin range(0, 5):
print(tu[item])
s = "djqwuidsdaf"
li = ["dgfhfsd", "djksd"]
tu = (123, 123, "asd")
v = list(tu) #元组转换列表
字典
info = {
"k1": "v1", # 键值对"k2": "v2"
}
v = dict.fromkeys([123, 'k1', 999], 123) # 根据序列创建字典,附上固定值
print(v)
v = info.get('k1') # 获取键值,key不存在,可以指定默认值
v = info.pop('k11', 90) # 删除并获取值,如没有返回指定值
v = info.popitem() # 随机删除项目
print(v)
v = info.setdefault('k1', '123') # 设置值,若key存在,获取当前key对应的值。
info.update({'k1': '1111', 'k3': 123})
info.update(k1='1121', k3=123) # 更新
print(info)
info = { # 键值对键值的key必须是确定值,hashable转换存2: "asd",
"k1": 'sda',
"k4": 'ssa',
"k2": "v1", # 键值对"k3": [22, 33, 44],
True: 'sda',
(10, 20): 'sda'
}
print(info)
del info[1] # 根据k值寻找操作for k, v in info.items():
print(k, v)