找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
  • 索引
    • 正向索引 0 1 2 3 4 5 6
    •    #a = a b c d e f g
    • 反向索引-7-6-5-4-3-2-1
  • 切片
    • 定义:切片是指对操作的对象截取其中一部分的操作,字符串、列表、元组都支持
    • 切片操作。
      • 切片的语法: [start index : end_ index : step]
      • 解释说明: start index表示起始索引
      • end_ index表示结束索引
      • step表示步长,步长不能为0,且默认值为1
    • 注意:切片操作是指按照步长,截取从起始索弓|到结束索引,但不包含结束索引(也就是结束索引减1)的所有元素。
    • 66.png
  • 字符串
    • 字符串是Python中最常用的数据类型。我们可以使用单引号"或者双弓|号""来创建字符串。
    • str1 = 'hello duoceshi'
    • str2 = "hello duoceshi"
    • 字符串常用函数:

      • 1、capitalize(): 首字符大写
        • 常用函数
        • st='hello'#定义字符串
        • #对字符中首字母小写改为大写
        • st1=st.capitalize()
        • print(st1)#Hello
        • print(st.capitalize())#Hello
      • 2、count(): 统计具体字符出现的次数
        • count 统计字符串中元素出现的次数
        • st='hello'#定义字符串
        • st1=st.count('l')#通过变量来接收值
        • print(st1)#2
        • print(st.count('l'))#2

      • 3、join():把集合中的字符按自定义的分隔符连接在一起
        • #join 拼接
        • st='hello'#定义字符串
        • print('*'.join(st))#h*e*l*l*o
        • list=['h','e','l','l','o']
        • print(list)#['h', 'e', 'l', 'l', 'o']
        • print(''.join(list))#hello
      • 4、split(): 把字符串通过指定标识符进行分割
        • st='he,llo'#定义字符串
        • print(type(st))#
        • st1=st.split(',')#把字符串中多余的元素分割掉pr
        • print(st1)#['he', 'llo']
        • #注:通过split分割后的结果类型是list(列表)
        • print(type(st1))#
        • print(''.join(st1))#hello
        • print(type(''.join(st1)))#
      • 5、strip(ab): 删除字符串开头和结尾有a或b的字符
        • strip :删除字符串的开头和结尾的元素
        • lstip:删除字符串开头的元素,不能删除结尾
        • rsertip:删除字符串结尾的元素,不能删开头的
        • st='hello'#定义字符串
        • print(st.strip('h'))#ello
        • print(st.strip('llo'))#he
        • print(st.lstrip('h'))#ello
        • print(st.rstrip('o'))#hell
      • 6、Istrip(ab): 删除字符串开头有ab序列字符
      • 7、rstrip(ab): 删除字符串结尾有ab序列字符

      • 8、startsvjith(): 判断字符串是否以什么开始
        • startswith 判断是否以某某开头,是返回true 反之false
        • endswith  判断是否以某某结尾,是返回true 反之false
        • st='hello'#定义字符串
        • print(st.startswith("he"))#True
        • print(st.startswith('e'))#False
        • print(st.endswith('llo'))#True
        • print(st.endswith('ll'))#False
      • 9、endswith(): 判断字符串是否以什么结束

      • 10、find()/rfind(): 字符串查询find()是左边开始查,rfind()从右边开始查, 结果显示下标
        • #获取指定值的索引值
        • find 从左开始查找
        • rfind 从右开始查找
        • st='hello'#定义字符串
        • print(st.find('l'))#2
        • print(st.rfind('l'))#3
      • 11、replace(): replacle(substring,newstring,max) substring表示被替换的字
        • st='hello'#定义字符串
        • print(st.replace('l','2'))#he22o 默认替换指定值,所有
        • print(st.replace('l','3',1))#he3lo 把l换成3 1表示次数
        • print(st.replace('hello','xiaoliu'))
        • print(st.replace(st,'小刘'))#小刘 替换所有
      • 12、lower(): 方法转换字符串中所有大写字符为小写
        • st='hello'#定义字符串
        • #把所有的小写字母变为大写
        • print(st.upper())#HELLO
      • 13、upper(): 将字符串中的所有小写字符转换为大写字母输出
        • st='HELLO'#定义字符串
        • #把所有的大写字母变为小写
        • print(st.lower())#hello
      • 14、isdigit(): 判断字符串中是否全为数字
        • #判断是否全部为数字,返回布尔值 是true 反之false
        • st1='he33o' #定义字符串
        • st2='123456' #定义字符串
        • print(st1.isdigit())#False 不满足全部为数字
        • print(st2.isdigit())#True 满足全部为数字
      • 15、isalpha(): 判断字符串中是否全为字母
        • #判断是否全部为字母,返回布尔值
        • st1='he33o' #定义字符串
        • st2='123456' #定义字符串
        • st3='HEllo'#定义字符串
        • print(st1.isalpha())#False
        • print(st2.isalpha())#False
        • print(st3.isalpha())#True
      • 16、isalnum(): 判断字符串当中是否全都为数字,全为字母,或者数字字母组合    返回布尔值: True和False 如果有特殊符号就是
      • 17、istitle(): 判断字符串中首字母是否为大写,其他是否为小写
        • *
          • 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
      • 18、判断一 个字符串是否 展示为都是大写或者都是小写
        • #isupper 判断全部为大写
        • #islower 判断全部为小写

  • pytnon中的列表
    • 定义:列表(list) 是一组有序存储的数据,也是python常见的序列之-,序列中的每个元素都分配一个索引, 第一个元素索引是0,第二个元素索引是1,依此类推。序列都可以进行的操作包括索引,切片,加,乘,检查成员
    • 列表表达符为: [ ]
    • 定义列表的两种方法:
      • 方法一: list1 = [1,2,3,4] #直接通过[]进行定义
      • 方法二: list2 = list('1234') #使用list()方法进行定义
    • 往列表中指定索引位添加数据
      • 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这个元素的索引位
    • 移除元素(元素名称)
      • 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]
    • reverse函数:列表元素反转                                     ·
      • 对列表反转输出(面试常问)
      • list1=[1,2,3,4,5]#定义列表
      • list1.reverse()#对列表进行反转
      • print(list1)#[5, 4, 3, 2, 1]
    • pop函数:
      • list1=[1,2,3,4,5]#定义列表
      • # list1.pop()#默认删除末尾的值
      • # print(list1)
      • #查看删除的值
      • #print(list1.pop())# 5直接打印被删除的值
      • list1.pop(4)#通过索引删除对应的值
      • print(list1)#[1, 2, 3, 4]

分享至 : QQ空间
收藏

0 个回复

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