二、字符串,元祖,列表等数据操作

一、字符串操作

 1 #replace将字符串中某个子字符串替换成另外一个字符串,但不改变原有的值
 2 a='helloword'
 3 b=a.replace('h','H')
 4 print(b)  #Helloword
5 #isspace判断字符串是否是纯空格
a=' \t\n\r'
print(a.isspace()) #True
6 #index返回字符串中子字符串的位置 7 indx=a.index('o') 8 print(indx) #4 9 10 #isdecimal判断字符串是否是纯数字组成 11 b='12231231' 12 print(b.isdecimal()) #True 13 14 #istitle判断字符串首字母是否大写 15 c='Hello' 16 print(c.istitle()) #返回True 17 18 #1、isupper判断字符串所有字母是否大写 2、upper将字符串全部改为大写,注意是不改变原来的字符串的 19 d='hello' 20 print(d.isupper()) #返回False 21 new=d.upper() 22 print(new) #HELLO 23 24 #1、islower判断字符串所有字母是否小写 2、lower将字符串全部改为小写,注意是不改变原来的字符串的 25 e='HELLO' 26 print(e.islower()) #返回False 27 new=e.lower() 28 print(new) #hello

二、列表操作

 1 #append向列表添加元素
 2 a=[]
 3 a.append('张三')
 4 print(a)
 5 
 6 #pop删除列表中元素,默认删除最后一个,若pop后面带数字则删除指定位置的元素;pop方法删除的同时并返回删除的元素
 7 a=['a','b','c','d']
 8 element=a.pop()  #删除了d元素
 9 print(a)
10 print(element)
11 element2=a.pop(0)
12 print(a)
13 
14 #remove也是删除某个元素,只不过里面跟的参数是要删除的元素对象
15 a=['a1','a2','a3']
16 a.remove('a2')
17 print(a)
18 
19 
20 #clear是清空列表
21 a=['b1','b2','b3','b4']
22 a.clear()
23 print(a)
24 
25 
26 #关键字del是删除列表,也可删除指定元素
27 a=['c1','c2','c3']
28 del a[1]  #删除指定元素
29 del a  #删除列表
30 print(a)

猜你喜欢

转载自www.cnblogs.com/lz-tester/p/9121887.html