找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

python 索引、切片、字符串函数、列表

[复制链接]

索引
正向索引、负向索引
   0 1 2 3 4 5 6  正向索引从0开始
  a=a b c d e f g
  -7-6-5-4-3-2-1  反向索引从-1开始


切片
[起始索引位:结束索引位:步长值]
切片语法中:包含起始不包含结束,步长值不能为0,可以为1(默认为1),1表示不隔位
st="abcdefghjkl"
print(st)
print(st[3]) #d 截取当前索引值为3对应的值
print(st[-3])#j 截取当前索引值为-3对应的值
print(st[1:4])#bcd 从索引值1开始取,到结束索引4,1-3有值
print(st[0:3])#abc 从索引值0开始取,到结束索引3,0-2有值
print(st[-4])#h
print(st[2:-1])#cdefghjk
print(st[:-1])#abcdefghjk
print(st[2:])#cdefghjkl
print(st[1:6])#bcdef
print(st[1:6:3])#be
print(st[1:6:2])#bdf
print(st[::1])#abcdefghjkl 为空则取所有
print(st[::-1])#lkjhgfedcba 把字符串的值进行反向输出
print(st[-2:-5:-1])#kjh
print(st[-3:-6:-2])#jg



字符串
字符串的定义用单引号或双引号
类型为:str
a='asadad' #通过单引号定义一个字符串
a1="adadada" #通过双引号定义一个字符串
print(type(a))#打印类型
print(type(a1))



字符串中常用函数
首字母改为大写 capitalize
a='asadad' #通过单引号定义一个字符串
a1=a.capitalize()
print(a1)#Asadad
print(a.capitalize())#Asadad

所有大写字母变成小写 lower
st='HELLOword' #定义一个字符串
print(st.lower()) #helloword

所有小写字母变成大写uppe
st='HELLOword' #定义一个字符串
print(st.upper()) #HELLOWORD

统计 count
统计字符串中元素出现的次数
a='asadad' #通过单引号定义一个字符串
a1=a.count("a")#通过变量来接收值
print(a1)#3
print(a.count("a"))#3

拼接 join
st="hello"
print("*".join(st))#h*e*l*l*o
l=['h','e','l','l','o']#['h', 'e', 'l', 'l', 'o']
print("".join(l))#hello

分割 split
st="he,llo" #定义一个字符串
print(type(st))#
a1=st.split(',')#把st字符串中多余的元素分割掉
print(a1)#['he', 'llo']
#注:通过split分割后的结果类型是list(列表)
print(type(a1))#
print(''.join(a1))#hello
print(type(''.join(a1)))#

删除字符 strip、lstrip、rstrip
strip:删除字符串的开头和结尾的元素
lstrip:删除字符串开头的元素,不能删除结尾的
rstrip:删除字符串结尾的元素,不能删除开头的
st="hello"#定义一个字符串
print(st.strip("he"))#llo
print(st.strip("llo"))#he
print(st.lstrip('h'))#ello
print(st.rstrip('o'))#hell

判断开头结尾 startswith、endswith
判断字符以什么开头或结尾,返回的是布尔值
startswith 判断是否以某某开头,是返回true,不是返回false
endswith 判断是否以某某结尾,是返回true,不是返回false
st="helloword" #定义一个字符串
print(st.startswith('he'))#True
print(st.startswith('e')) #False
print(st.endswith('rd')) #True
print(st.endswith('r')) #False

判断是否全数字 isdigit
返回布尔值
st1='h12llo' #定义一个字符串
st2='12345' #定义一个字符串
print(st1.isdigit()) #False 不满足全部为数字
print(st2.isdigit()) #True 满足全部为数字

判断是否全字母 isalpha
返回布尔值
st1='h12llo' #定义一个字符串
st2='12345' #定义一个字符串
st3='hello' #定义一个字符串
print(st1.isalpha()) #False 不满足全部为字母
print(st2.isalpha()) #False 不满足全部为字母
print(st3.isalpha()) #True 满足全部为字母

判断是否为全数字或全字母或数字字母组合 isalnum
有特殊符号时为否,返回布尔值
st1='h12llo' #定义一个字符串
st2='12345' #定义一个字符串
st3='hello' #定义一个字符串
st4='12h@h*' #定义一个字符串
print(st1.isalnum()) #True
print(st2.isalnum()) #True
print(st3.isalnum()) #True
print(st4.isalnum()) #False

判断首字母是否为大写,其他为小写 istitle
首字母为大写,后面接数字和特殊符号也为对,返回布尔值
st1='A'
st2='AA'
st3='A1234'
st4='Adfd'
st5='A2@'
st6='ass'
st7='a12'
print(st1.istitle()) #True
print(st2.istitle()) #False
print(st3.istitle()) #True
print(st4.istitle()) #True
print(st5.istitle()) #True
print(st6.istitle()) #False
print(st7.istitle()) #False

判断字符串开头是否都时大写或者小写 isupper、islower
isupper 判断全部为大写
islower 判断全部为小写
数字不影响只针对字母,返回布尔值
st1='sfecxsf' #定义字符串
print(st1.isupper()) #False 判断是否全部为大写
print(st1.islower()) #True 判断是否全部为小写
st2='asdasDDD' #定义字符串
print(st2.isupper()) #False 判断是否全部为大写
print(st2.islower()) #False 判断是否全部为小写
st3='ASDFSD' #定义字符串
print(st3.isupper()) #True 判断是否全部为大写
print(st3.islower()) #False 判断是否全部为小写
st4='asdsad1234'
print(st4.isupper()) #False 判断是否全部为大写
print(st4.islower()) #True 判断是否全部为小写
st5='DSFDS12345'
print(st5.isupper()) #True 判断是否全部为大写
print(st5.islower()) #False 判断是否全部为小写

获取指定索引值 find、rfind
find:获取指定值的索引值,从左开始(得到的都是正向索引值)
rfind:获取指定值的索引值,从右开始
st="helloword" #定义一个字符串
print(st.find('o')) #4
print(st.rfind('o'))#6

替换replace
st="helloword" #定义一个字符串
print(st.replace('l','2')) #he22oword 默认替指定值所有
print(st.replace('l','3',1)) #he3loword 把l换成3 1表示次数
print(st.replace("helloword",'xiaoyao')) #把xiaoyao替换所有
print(st.replace(st,'小瑶')) #小瑶替换所有


列表 list
[ ]中括号来定义列表,也可以使用lise()
列表中每一个元素都有一个索引
list=['123'] #定义一个列表
print(type(list)) #
print(list)
list1=list('123')
print(list1)
print(type(list1))
a='2' #定义字符串
print(type(a)) #
c=list(a) #通过list把字符串变为列表
print(type(c)) #
列表中每一个元素都有一个索引
list2=['hello',1,2,3,4,5,6]
print(list2[0]) #hello 通过索引取值
print(list2[4]) #4

替换
list2=['hello',1,2,3,4,5,6] #定义列表
list2[2]='xiaoyao' #通过索引把对应索引值进行替换
print(list2) #['hello', 1, 'xiaoyao', 3, 4, 5, 6]
#python3中可以直接打印中文,python2中不可以直接打印中文

添加元素 append
a=[1,2,3] #定义列表
a.append('hello') #添加元素hello,先加再打印
print(a) #[1, 2, 3, 'hello']

拼接列表 extend
a1=[1,2,3] #定义列表
a2=[4,5,6] #定义列表
a1.extend(a2) #把a2拼到a1里面
print(a1) #[1, 2, 3, 4, 5, 6]

指定位置插入数据 insert
#往列表指定索引位添加数据(插入在索引位前)
a1=[1,2,3] #定义列表
a1.insert(2,'hello') #在索引位2插入hello
print(a1) #[1, 2, 'hello', 3]

查找列表中指定的元素取对应的索引 index
a1=[1,2,3,4,5] #定义列表
print(a1.index(1)) #0 查询1这个元素的索引位为0

移除元素 remov、del
通过元素删除用remov,通过索引删除用del,如果有多个相同的元素会删除前面的那个
移除元素
a1=[1,2,3,4,5] #定义列表
a1.remove(1) #把列表中的1这个元素删除
print(a1) #[2, 3, 4, 5]
删除元素(通过索引值)
a1=[1,2,3,4,5] #定义列表
del a1[0] #通过del来删除索引值对应的数据
print(a1) #[2, 3, 4, 5]

删除末尾 pop
默认移除列表最后一位元素,并返回元素的值
a1=[1,2,3,4,5] #定义一个列表
a1.pop() #默认删除末尾的值
print(a1) #[1, 2, 3, 4]
a2=[1,2,3,4,5] #定义一个列表
print(a2.pop()) #5 打印被删除的值
a3=[1,2,3,4,5] #定义一个列表
a3.pop(0) #通过索引来删除索引对应的数据
print(a3) #[2, 3, 4, 5]

升序 sort 降序 sorted(True,False)
升序排序
a1=[5,2,1,3,6,4,7] #定义列表
a1.sort() #对当前这个列表进行升序排序
print(a1) #[1, 2, 3, 4, 5, 6, 7]
降序排序
a1=[5,2,1,3,6,4,7] #定义列表
实现降序排序:True
print(sorted(a1,reverse=True)) #[7, 6, 5, 4, 3, 2, 1]
实现升序排序:False
print(sorted(a1,reverse=False)) #[1, 2, 3, 4, 5, 6, 7]

反转输出 reverse(面试常问)
a1=[1,2,3,4,5] #定义列表
a1.reverse() #对列表进行反转
print(a1) #[5, 4, 3, 2, 1]
对字符串的反转
a2='12345' #定义字符串
print(a2[::-1]) #54321

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册