标签:-- import 位置 pytho %s 添加 index 元素 end
一、常用字符串操作
str_1="hello,python,lower,‘666‘,‘777‘" #upper()把字母编程大写 #lower()把字母变成小写 print(str_1.upper()) print(str_1.lower())
str_1 = "hello,python,lower,‘666‘,‘777‘" print(str_1.split(‘l‘)) # [‘he‘, ‘‘, ‘o,python,‘, "ower,‘666‘,‘777‘"] print(str_1.split(‘,‘)) # [‘hello‘, ‘python‘, ‘lower‘, "‘666‘", "‘777‘"] print(str_1.split(‘,‘, 2)) # [‘hello‘, ‘python‘, "lower,‘666‘,‘777‘"]
str_2="6hello,python,lower,‘666‘,‘7776‘" str_3="8888hello,python,lower,‘666‘,‘77768888‘" print(str_2.split(‘6‘)) # [‘‘, "hello,python,lower,‘", ‘‘, ‘‘, "‘,‘777", "‘"] print(str_3.split(‘8‘)) # [‘‘, ‘‘, ‘‘, ‘‘, "hello,python,lower,‘666‘,‘7776", ‘‘, ‘‘, ‘‘, "‘"]
str_2="6hello,python,lower,‘666‘,‘7776‘" print(‘e6‘.join(str_2)) 控制台输出: 6e6he6ee6le6le6oe6,e6pe6ye6te6he6oe6ne6,e6le6oe6we6ee6re6,e6‘e66e66e66e6‘e6,e6‘e67e67e67e66e6‘
备注:常使用来对字符进行拼接,或者将列表数据类型转换成字符串数据类型
new_numbers = [] for n in List: new_numbers.append(str(n)) numbers = new_numbers num = ‘‘.join(numbers) print("加密后输出的整数为:%s" % num)
二、列表的处理
# 切片操作 name = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] print(‘1:‘, name) print(‘2:‘, name[0], name[2]) print(‘3:‘, name[1:3]) #切片 print(‘4:‘, name[-2:]) #切片 print(‘5:‘, name[::-1]) #切片的结果是倒序排列 print(‘6:‘, name[:-1]) #切片的结果是,输出到倒数第二位 -1-1=-2 控制台输出 1: [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 2: trek redline 3: [‘cannondale‘, ‘redline‘] 4: [‘specialized‘, ‘trek‘] 5: [‘trek‘, ‘specialized‘, ‘redline‘, ‘cannondale‘, ‘trek‘] 6: [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]
name = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] name.append("guapi") #在列表中添加新的元素,附加到列表的末尾 name.insert(1, "nihao") #在列表的任何位置添加元素 name.insert(3, "doudou") name[2] = "guyun" #修改列表中的元素
name = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] print("1:", name.index("trek")) print("2:", name[name.index("trek")]) 控制台输出: 1: 0 2: trek
name.remove("guyun") #直接删除元素 del name[1] #根据元素的下标,来删除元素 name.pop() #不输入下标,删除默认最后一个值 name.pop(1)
#name.clear() #清空列表 #print(name.count("trek")) #count 统计个数
name.reverse() #reverse 反转 print(name) name.sort() #sort 排序 (按照ASCTT中的排序方法) print(name) 控制台输出: [‘trek‘, ‘specialized‘, ‘redline‘, ‘cannondale‘, ‘trek‘] [‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘, ‘trek‘]
list_1=[1,2,‘666‘] list_1[2]=‘python‘ #更改为字符串 print(list_1) list_1[2]=666 #更改为整形
name = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] name3 = name.copy() print(‘1:name--‘, name) print(‘2:name3--‘, name3) name.insert(1, "susu") print(‘3:name--‘, name) print(‘4:name3--‘, name3) import copy name4 = copy.copy(name) #浅copy print(‘5:name--‘, name) print(‘6:name4--‘, name4) name5 = copy.deepcopy(name) #深copy print(‘7:name--‘, name) print(‘8:name5--‘, name5) name6 = name[:] #浅copy name7 = list(name) #浅copy print(‘9:name6--‘, name6) print(‘10:name7--‘, name7) 控制台输出: 1:name-- [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 2:name3-- [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 3:name-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 4:name3-- [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 5:name-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 6:name4-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 7:name-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 8:name5-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 9:name6-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘] 10:name7-- [‘trek‘, ‘susu‘, ‘cannondale‘, ‘redline‘, ‘specialized‘, ‘trek‘]
python.day.04——常用数据类型(一):str&List
标签:-- import 位置 pytho %s 添加 index 元素 end
原文地址:https://www.cnblogs.com/Rosay9204781/p/9157307.html