字符串、列表的基本操作
一、可变与不可变
可变:值改变,但是id不变,证明就是在改变原值,是可变类型。它的原理是在内存里有一个值,然后这个值发生了改变,意为id地址是同一个,没有变化
l=['a','b'] print(id(l)) # 34743240 l[0]='A' print(l) # ['A','b'] print(id(l)) # 34743240
不可变:值改变,但是id也跟着变,证明是产生了新的值,是不可变类型。它的原理是在内存里有一个值,然后又申请了另一个地址,则id地址不同。称为不可变类型。
x = 9 print(id(x)) # 1580166880 x = 10 print(id(x)) # 1580166912
二、数字类型
1、整形 基本使用 用途:记录数字,年纪,手机号 定义方式 age = 18 age = int(18) print(age,type(age))
数据类型转换: 只能将纯数字的字符串转成int a = int('123456') print(a,type(a)) a = int('123') print(a,type(a ))
总结: 存一个值 不可变(值变,id就变。) x = 9 print(id(x)) # 1580166880 x = 10 print(id(x)) # 1580166912
三、字符串
基本使用 用途:记录描述性质的状态 定义方式:用单引号,双引号,三引号中包含一系列字符 l = 'abcde' l = str(l) print(l,type(l )) # abcde <class 'str'> name = 'hello' name = str(name) print(name,type(name)) # hello <class 'str'> 数据类型转换:所有类型都可以被str转成字符串类型 res=str([1,2,3]) print(res,type(res)) # [1, 2, 3] <class 'str'> l = [1,2,3,4,5,6] l = str([1,2,3,4,5,6]) print(l,type(l)) # [1, 2, 3, 4, 5, 6] <class 'str'> 常用操作+内置的方法 1、按索引取值(正向取+反向取) :只能取 name = 'helloworld' print(name[0],name[1],name[2],name[3],name[4],type(name )) # h e l l o <class 'str'> print(name[-1],name[-2],name[-3],name[-4],name[-5],type(name )) # d l r o w <class 'str'> print(name[0]) print(name[1]) print(name[2]) print(name[3])
2、切片(顾头不顾尾,步长) name = 'helloworld' mag = name[0:10] # 0 1 2 3 4 5 6 7 print(mag) # helloworld mag = name[::] print(mag) # helloworld name = 'helloworld' mag = name[-1:-11:-1] # -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 print(mag) # dlrowolleh mag = name[-1::-1] print(mag) # dlrowolleh mag = name[::-1] print(mag) # dlrowolleh 3、长度len name = 'helloworld' print(len(name)) # 10 4、成员运算in和not in:判断一个子字符串是否存在于大字符串中 name = 'helloworld' print('i'in name) # False print('hell'in name) # True 5、移除空白strip: 用来去除字符串左右两边的字符,不指定默认去除的是空格 name = input('your username:>>>').strip() password = input('your passworld:>>>').strip() if name == 'helloworld'and password =='1121': print('login successful') else: print('error') name = ' helloworld' name = name.strip() print(name) # helloworld name = '&*helloworld' name = name.strip('&*@#$%') print(name) # helloworld name = '&*hello####world&*' name = name.strip('#&*@#$%') print(name) # hello####world 注意 hello与world左边和右边的字符会消失,但是中间的字符还存在。 name = name.strip('&*@#$%') print(name) print('&*helloworld@#$%'.strip('&*@#$%')) # helloworld 6、切分split:针对有规律的字符串,按照某种分隔符切成列表 name = 'jerry:25:male' name = name.split(':') print(name) # ['jerry', '25', 'male'] name = 'jerry:25:male' name = name.split(':',1) print(name) # ['jerry', '25:male']
7、join用法。用:号作连接符号将纯字符串的列表拼接成一个字符串 name = ['jerry','25','male'] name = name[0]+':'+name[1]+':'+name[2] print(name) # jerry:25:male name = ['jerry','25','male'] name = ':'.join(name) print(name) # jerry:25:male 8、循环 for name in 'hello': print(name)
1、strip,lstrip,rstrip 移除空格strip:移除字符串左右两边的字符 lstrip:移除字符串左边的字符 rstrip:移除字符串右边的字符 print('******tom***********'.strip('*')) # tom print('******tom***********'.lstrip('*')) # tom*********** print('******tom***********'.rstrip('*')) # ******tom 2、lower,upper lower:大写字母变小写 upper:小写字母变大写 print('TOM123'.lower()) # tom123 print('tom123'.upper()) # TOM123 name = 'TOM' print(name.lower()) name = 'tom' print(name.upper())
3、startswith,endswith name ='tom is me' print(name.startswith('t')) # True 首字母 print(name.endswith('e')) # True 尾字母 print(name.endswith('is')) # False
4、format的三种玩。格式化输出。和%s类似。但format可以任意位置的 name ='my name is %s my age is %s' %('tom',25) print(name) # my name is tom my age is 25 # 方法一 tag = 'my name is {name} my age is {age}'.format(name = 'tom',age =25 ) print(tag) # my name is tom my age is 25 了解 # 方法二 tag ='my name is {0} my age is {1}'.format('tom',25) print(tag) # my name is tom my age is 25 # 方法三 tag='my name is {0}{1} my age is {1}{1}{1}{1}'.format('tom',25) print(tag) # my name is tom25 my age is 25252525 5、split,rsplit split:切分(从左往右) rsplit:切分(从右往左) msg='a:b:c:d' print(msg.split(':',1)) # ['a', 'b:c:d'] print(msg.rsplit(':',1)) # ['a:b:c', 'd'] name = 'jerry:tommy:judy:una' print(name.split(':',1)) # ['jerry', 'tommy:judy:una'] print(name.rsplit(':',1)) # ['jerry:tommy:judy', 'una']
6、replace:替换 name = 'jerry say :my name is jerry, a college student' print(name.replace('jerry','man',1)) # man say :my name is jerry, a college student
7、isdigit:如果字符串是由纯数字组成的,则返回True print('123123'.isdigit()) Ture print('123123 '.isdigit()) False print('123123asdf'.isdigit()) False print('12312.3'.isdigit()) False
字符串类型总结: 存一个值 有序 不可变(值改变,但是id也跟着变,证明是产生了新的值,是不可变类
四、列表
按照位置记录多个任意类型的值。并且可以取出指定位置的值。 通过索引对应值,从0开始,代表的是位置。 定义:在中括号内用逗号隔开任意类型的值 name = ['jerry', 25, 175.5, [120, 5000]] 数据类型转换:但凡能够被for循环的数据类型都可以传给list,被其转换成列表 name = list('hello') print(name) # ['h', 'e', 'l', 'l', 'o'] name = ['jerry', 25, 175.5, [120, 5000]] name = list(name) print(name) # ['jerry', 25, 175.5, [120, 5000]] res = list({'a': 1, 'b': 2, 'c': 3}) print(res) # ['a', 'b', 'c']
常用操作 + 内置方法 1、按索引存取值(正向存取+反向存取):即可存也可以取 name = ['jerry', 25, 175.5, [120, 5000]] print(name[0], name[1], name[2], name[3]) # jerry 25 175.5 [120, 5000] print(name[-1], name[-2], name[-3], name[-4]) # [120, 5000] 175.5 25 jerry # 注意注意注意:对于不存在的索引会报错 可存: dic = {"k1":111} dic['k2'] = 2222 print(dic) # {'k1': 111, 'k2': 2222} 2、切片(顾头不顾尾,步长) name = ['jerry', 25, 175.5, [120, 5000]] print(name[0:4:1]) # ['jerry', 25, 175.5, [120, 5000]] print(name[::1]) # ['jerry', 25, 175.5, [120, 5000]] print(name[0::1]) # ['jerry', 25, 175.5, [120, 5000]] print(name[-1::-1]) # [[120, 5000], 175.5, 25, 'jerry'] print(name[::-1]) # [[120, 5000], 175.5, 25, 'jerry'] print(name[:]) # ['jerry', 25, 175.5, [120, 5000]] print(name[::]) # ['jerry', 25, 175.5, [120, 5000]]
3、长度len name = ['jerry', 25, 175.5, [120, 5000]] print(len(name)) # 4 4、成员运算in和not in name = ['jerry', 25, 175.5, [120, 5000]] print('jerry' in name) # True print('tommy' not in name) # True 5、追加append和插入insert ①追加append:只能往后追加 name = ['jerry', 25, 175.5, [120, 5000]] name.append('abc') print(name) # ['jerry', 25, 175.5, [120, 5000], 'abc'] name = ['jerry', 'tommy', 'judy'] name.append('una') print(name) # ['jerry', 'tommy', 'judy', 'una'] ②插入insert:在指定位置插入 name = ['jerry', 25, 175.5, [120, 5000]] name.insert(0, '100000') print(name) # ['100000', 'jerry', 25, 175.5, [120, 5000]] name = ['jerry', 25, 175.5, [120, 5000]] name.insert(1, '100000') print(name) # [ 'jerry','10000', 25, 175.5, [120, 5000]] name = ['jerry', 25, 175.5, [120, 5000]] name.insert(4, '100000') print(name) # ['jerry', 25, 175.5, [120, 5000], '100000'] 6、删除 ①del:不是列表独有的删除,是通用的删除。del没有返回值,只是单纯的删除。 name = ['jerry', 25, 175.5, [120, 5000]] del name[0] print(name) # [25, 175.5, [120, 5000]] ②remove:指定要删除的那个元素。和del一样没有返回值,都只是单纯的删除 name = ['a', 'bbb', 'c', 'd', 'e'] name.remove('bbb') print(name) # ['a', 'c', 'd', 'e'] ③pop:(指定要删除的那个元素的索引)。有返回值,返回刚刚删掉的那个元素。 意为取走 name = ['a', 'bbb', 'c', 'd', 'e'] name.pop() print(name) # ['a', 'bbb', 'c', 'd'] # 不指定位置,默认删除最后一个 name = ['a', 'bbb', 'c', 'd', 'e'] name.pop(0) print(name) # ['bbb', 'c', 'd', 'e'] 7、循环 name = ['jerry', 25, 175.5, [120, 5000]] for item in name: print(item) # 结果为 jerry 25175.5 [120, 5000]
8、clear:清除 name = ['aaa', 'bb', 345] name.clear() print(name) # [] 9、extend加多个元素 name = ['aaa', 'bb', 345] name.extend(['tommy', 120, 175.5]) print(name) # ['aaa', 'bb', 345, 'tommy', 120, 175.5] 10、.reverse:反过来 name = ['aaa', 'bb', 345] name.reverse() print(name) # [345, 'bb', 'aaa'] 11、sort 排序:只有在类中中所有元素都是同种类型的情况下才能用sort排序(升序) name = [1, 100, 1000] name.sort() print(name) # [1, 100, 1000] name = ['z', 'd', 'a'] name.sort() print(name) # ['a', 'd', 'z'] 列表类型总结: 存多个值 有序 可变
五、练习
1、写代码,有如下变量,请按照要求实现每个功能 name = " aleX" 1) 移除 name 变量对应的值两边的空格,并输出处理结果 name = " aleX" print(name.strip()) 2) 判断 name 变量对应的值是否以 "al" 开头,并输出结果 name = " aleX" print(name.startswith('al')) 3) 判断 name 变量对应的值是否以 "X" 结尾,并输出结果 name = " aleX" print(name.endswith('X')) 4) 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果 name = " aleX" print(name.replace('l', 'p', 3)) 5) 将 name 变量对应的值根据 “l” 分割,并输出结果。 name = " aleX" print(name.split('l')) 6) 将 name 变量对应的值变大写,并输出结果 name = " aleX" print(name.upper()) 7) 将 name 变量对应的值变小写,并输出结果 name = " aleX" print(name.lower()) 8) 请输出 name 变量对应的值的第 2 个字符? name = " aleX" print(name[1]) 9) 请输出 name 变量对应的值的前 3 个字符? name = " aleX" print(name[:3]) 10) 请输出 name 变量对应的值的后 2 个字符? name = " aleX" print(name[-2:]) 11) 请输出 name 变量对应的值中 “e” 所在索引位置? name = " aleX" print(name.index('e')) 12) 获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。 name = " aleX" print(name[:4:1])