找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 武汉18期谭袁 于 2022-8-25 21:04 编辑

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

切片
[起始索引位:结束索引位:步长值]
切片语法中:包含起始不包含结束,步长值不能为0,可以为1(默认为1),1表示不隔位
a='abcdefg'
print(a)
print(a[3])   #d   截取当前索引值为3对应的值
print(a[-3])  #e   截取当前索引值为-3对应的值
print(a[3:5])  #de  从索引值3开始取,到结束索引5    3-4有值
print(a[1:4])  #bcd  从索引值1开始取,到结束索引4    1-2-3有值
print(a[2:-1])  #cdef
print(a[ :-1])  #abcdef
print(a[0:])  #abcdefg
print( a[1:6])  #bcdef
print(a[1:6:2])  #bdf  切片
print(a[ 1:6:3])  #be  切片
print(a[ : : 1])  #abcdefg.为空则取所有
print(a[ : : -1])  #gfedcba 把字符串的值进行反向输出
print(a[ -2:-5:-1])  #fed
print(a[ -3:-6:-2])   #ec
起始索引及结束索引位控制取值范围,步长值控制取值方向及隔位取值
字符串
字符串的定义用单引号或双引号
类型:str
a='aassaas'    #通过单引号定义一个字符串
a1=" adasda "   #通过双引号定义一个字符串
print(type(a))    #打印类型为class ’str'
print(type(a1))


常用函数
st= ' hello'#定义字符串
#对字符串首字母小写改为大写
st1=st.capitalize()
print(st1)
#print(st1 ) #Hello
# print( st.capitalize( ) ) #HeLLo


count统计字符串中元素出现的次数
st='hello'   #定义字符串
st1=st. count('l')   #通过变量来接收值
print (st1) #2
print (st.count('l'))#2


#join拼接
st='hello'#定义字符串
print(‘ * ’ .join(st) )#h*e*1*1*o

list=[ 'h', 'e', 'l', 'l', 'o']
print(list)#['h',''e', 'l ','l','o']
print('' .join(list) ) #hello     取消分隔符(如果是’list' 类型则会自动变成 ‘str' 类型

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


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


判断字符以什么开始或结束,返回的是布尔值
startswith   判断是否以XX开头,是返回true,反之false
endswith    判断是否以XX结尾, 是返回true,反之false
st='hello'#定义字符串
print (st.startswith ( "he" ) ) #True
print (st .startswith ( 'e') ) #False
print (st.endswith ( 'llo'))  #True
print (st.endswith ( 'll'))  #False


获取指定值的索引值

find   从左开始查找
rfind  从右开始查找

st='hello'#定义字符串
print(st. find('l')) #2
print(st. rfind('l') ) #3


替换函数replace
st= 'hel1o'  #定义字符串
print(st. replace('1','2'))  #  he22o   默认替换指定值,所有
print(st.replace('1','3',1))  #he3Lo 把L换成3  1表示次数
print(st. replace( ' hello',xiaoliu'))
print(st.replace(st, '小刘'))   #小刘替换所有


st='hello'#定义字符串
#把所有的小写字母变为大写  
print (st. upper () )    #HELLO

st='HELLO' #定义字符串
#把所有的大写字母变为小写.
print (st. lower () )   #hel1o

判断是否全部为数字 返回布尔值  是true  反之false
st1='he33o'  #定义字符串
st2='123456'  #定义字符串
print( st1.isdigit () )   #false 不满足全部为数字
print( st2.isdigit () )   #true 满足全部为数字

判断是否全部为字母 返回布尔值  是true
st1='he33o'  #定义字符串
st2='123456'  #定义字符串
st3='HELLO'   #定义字符串
print( st1.isalpha () )#false
print( st2.isalpha () )#false
print( st3.isalpha () ) #true

#判断是否为全部数字或全字母或数字字母组合,有特殊符号时为否 返回布尔值
st1='he33o'  #定义字符串
st2='123456'  #定义字符串
st3='HELLO'   #定义字符串
st4='he#llo'  #定义字符串
print(st1.isalnum())  #True
print(st2.isalnum())  #True
print(st3.isalnum())  #True
print(st4.isalnum())  #false

#判断首写字母是否为大写 其他为小写
st1='A'
st2='AA'
st3='A1234'
st4='Aaaa'
st5='Aaa@'
st6='aaaaa'

print(st1.istitle())  #true
print(st2.istitle())  #False
print(st3.istitle())  #true
print(st4.istitle())  #true
print(st5.istitle())  #true
print(st6.istitle())  #False

#判断字符串全部为大写或小写
#isupper 判断全部为大写
#islower 判断全部为小写

st1='szfddsfds' #定义字符串
print(st1.isupper())#False 判断全部为大写
print(st1.islower())#True  判断全部为小写

st2='sssDDDS'#定义字符串
print(st2.isupper())#False
print(st2.islower())#False

st3='ass1234'
print(st3.isupper())#False
print(st3.islower())#True

st4='AAA1234'
print(st4.isupper())#True
print(st4.islower())#False

st5='AAAAA'
print(st5.isupper())#True
print(st5.islower())#False
列表
【】括号来定义
list =['123']#定义列表
print(type(list))#
print(list)#['123']

list1=list('234')
print(type(list1))#

a ='2'#定义字符串
print(type(a))  #
c=list(a)#通过list把字符串变为列表
print(type(c))  #

list=['hello','1','2','3','4','5','6']#定义列表
#列表中的每个元素都有一个索引
print(list[0])#hello
print(list[4])#4

list=['hello','1','2','3','4','5','6']#定义列表
list[2]="xiaoliu" #通过索引把对应索引的值进行替换
print(list)#['hello', '1', 'xiaoliu', '3', '4', '5', '6']

#python3中可以直接打印中文,python2中不可以直接打印中文


list=[1,2,3,4]#定义列表
print(type(list))#
list.append('hello')#添加元素
print(list)#[1, 2, 3, 4, 'hello']
列表拼接
list1=[1,2,3,4]#定义列表
list2=[5,6,7,8]#定义列表
list1.extend(list2)
print(list1)#[1, 2, 3, 4, 5, 6, 7, 8]


往列表中指定索引位添加数据
list1=[1,2,3,4]#定义列表
list1.insert(2,'hello')#
print(list1)#[1, 2, 'hello', 3, 4]


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

移除元素remove(元素名称)
list1=[1,2,3,4,5]#定义列表
list1.remove(5)#把列表中的5这个元素进行删除
print(list1)#[1, 2, 3, 4]




删除元素(索引值)
list2=[1,2,3,4,5]#定义列表
del list2[4]  #通过del 来删除索引值对应的数据
print(list2)  #[1, 2, 3, 4]


升序排序
list1=[5,1,4,3,2]#定义一个列表
list1.sort()#对当前这个列表进行升序排序
print(list1)#[1, 2, 3, 4, 5]


降序排序
list2=[5,1,4,3,2]#定义一个列表
#降序排序:True
print(sorted(list2,reverse=True))#[5, 4, 3, 2, 1]
#升序排序:False
print(sorted(list2,reverse=False))#[1, 2, 3, 4, 5]

对列表反转输出(面试常问)
list1=[1,2,3,4,5]#定义列表
list1.reverse()#对列表进行反转
print(list1)#[5, 4, 3, 2, 1]

list1=[1,2,3,4,5]#定义列表
# list1.pop()#默认删除末尾的值
#print(list1.pop())  # 5直接打印被删除的值
list1.pop(4)#通过索引删除对应的值
print(list1)#[1, 2, 3, 4]















分享至 : QQ空间
收藏

0 个回复

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