• python字符串,列表常用操作


    24天养成一个好习惯,第五天!

    一、字符串需要掌握的操作

    1.取值(索引取值)需要注意的是只能取,不能改

    1 msg = 'hello world'
    2 print(msg[4])

    2.切片(顾头不顾尾)

    1 msg='hello world'#就是从一个大的字符串中切出一个全新的字符
    2 print(msg[0:5])
    3 print(msg[::-1])#倒着取字符串里的数

    3.长度len

    1 msg = 'hello world'
    2 print(len(msg))#len就是取字符串中所有字符的长度

    4.成员运算 in和not in(判断一个字符串是否在一个大字符串中)

    1 print('dsb' in 'xxx is dsb')#结果为True,
    2 print('xxx' not in 'abc is dsb')#结果为True

    5.strip,lstrip,rstrip

    默认是去除字符串两边的空格,注意中间的空格无法去除

    除了去除空格还可以去除别的内容

     

    lstrip和rstrip与strip的区别就是,rstrip是去除右边需要去除的字符,而lstrip是去除左边需要去除的字符

    6.切分split和join

    将一个字符串按特定的符号切分成多个列表的形式,后面的数字代表切分的个数,至于,split和rsplit的区别就是从左切和从右切的区别

    join:将切分的字符串再次拼接再一起

    7.lower,upper,title

    lower:将字符串中的所有字母变为小写

    upper:将字符串中的所有字母变为大写

    title:将字符串中的所有字母变为首字母大写

     

    8,startwith,endwith

    startswith()函数判断文本是否以某个字符开始,endswith()函数判断文本是否以某个字符结束。

     

     9.replace

    replace中有三个参数,第一个为old(要替换的字符),第二个为new(替换成什么字符),第三个为需要替换的个数

    10.isdigit

    判断这个字符串是否为纯数字

    需要了解的内容:

    1.find,rfind,index,rindex

     1 #find
     2 str1 = "Zfj adf id tedafd!!!"
     3 str2 = "id"
     4 print(str1.find(str2))
     5 print(str1.find(str2, 5))
     6 print(str1.find(str2, 10))
     7 print(str1.rfind(str2))
     8 #index
     9 str1 = "Zfj adf id tedafd!!!"
    10 str2 = "id"
    11 print(str1.index(str2))
    12 print(str1.rindex(str2))
    13 print(str1.index(str2,10))#错误,与find方法大致一样,只是index方法str不在str1中会报一个异常。

    2.center,ljust,rjust,zfill

    1 name = '123'
    2 print(name.center(20, '*'))
    3 print(name.ljust(20, '*'))
    4 print(name.rjust(20, '*'))
    5 print(name.zfill(20))#返回指定长度的字符串,原字符串右对齐,前面填充0。

     3.expandtabs:把字符串中的 tab 符号(' ')转为空格,tab 符号(' ')默认的空格数是 8。

     captalize:把字符串里的首字母变为大写

     swapcase:把字符串里的字母大小写转换

    4.其它is

     1 name = 'zfj1234中'
     2 print(name.isalnum()) #字符串由字母或数字组成 True
     3 print(name.isalpha()) #字符串只由字母组成 True
     4 n1 = '123'
     5 n2 = ''
     6 n3 = ''
     7 
     8 print(n1.isnumeric()) #True
     9 print(n2.isnumeric()) #True
    10 print(n3.isnumeric()) #True

    二、列表常见操作

    1.按索引取值

    l1 = ['abc', 'cdf']
    print(l[11])

    2.切片

    l1 = ['abc', 'cdf', 'sdf', '11']
    print(l1[2:5])

    3.in,not in ,len 与字符串大致无异

    4.追加

    通过append和insert添加数据至列表,append只能添加至列表的末尾,而insert可以指定位置添加

    5.删除

    通过del和remove删除列表中的数据,del通过索引删除,remove直接通过列表中的值删除

    l1 = ['abc', 'cdf', 'sdf', '11']
    # del l1[0]
    # print(l1)
    l1.remove('abc')
    print(l1)
    l1.pop()#默认最后一个,可以根据索引删除

    6.改

    l1 = ['abc', 'cdf', 'sdf', '11']
    l1[0] = 'zfj'
    print(l1)

    补充:

    #队列:先进先出
    l = []
    # 入队
    # l.append('first')
    # l.append('second')
    # l.append('third')
    # print(l)
    # 出队
    # print(l.pop(0))
    # print(l.pop(0))
    # print(l.pop(0))
    
    #堆栈:先进后出
    # 入栈:
    # l.append('first')
    # l.append('second')
    # l.append('third')
    # 出栈:
    # print(l.pop())
    # print(l.pop())
    # print(l.pop())
    焚膏油以继晷,恒兀兀以穷年。
  • 相关阅读:
    POJ 1062 昂贵的聘礼(最短路)题解
    BZOj 墨墨的等式(转化为最短路)题解
    BZOJ 2763 飞行路线(分层图最短路)题解
    HDU 6342 Expression in Memories(模拟)多校题解
    codeforces 543B Destroying Roads
    codeforces 639B Bear and Forgotten Tree 3
    codeforces 645D Robot Rapping Results Report
    codeforces 702E Analysis of Pathes in Functional Graph
    codeforces 721C journey
    codeforces 711D Directed Roads
  • 原文地址:https://www.cnblogs.com/mangM/p/9360660.html
Copyright © 2020-2023  润新知