• python中strip、startswith、endswith


    strip(rm)用来删除元素内的空白符:

    rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符

    startswith、endswith用来查找开头或结尾条件的元素

    例子:

    复制代码
     1 li = ["alec", " aric", "Alex", "Tony", "rain"]
     2 tu = ("alec", " aric", "Alex", "Tony", "rain") 
     3 dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
     4 for i in li:
     5     b = i.strip()
     6     if (b.startswith("a") or b.startswith("A")) and b.endswith("c"):
     7         print(b)
     8 
     9 for i in tu:
    10     b = i.strip()
    11     if (b.startswith("a") or b.startswith("A")) and b.endswith("c"):
    12         print(b)
    13         
    14 for i in dic:
    15     b = dic[i].strip()
    16     if (b.startswith("a") or b.startswith("A")) and b.endswith("c"):
    17         print(b)
    复制代码

    上面代码:查找以 a或A开头并且以 c 结尾的所有元素,并输出

    输出结果:

    alec
    aric
    alec
    aric
    aric
  • 相关阅读:
    P2009 跑步
    P3916 图的遍历
    P2865 [USACO06NOV]路障Roadblocks
    P2820 局域网
    P2176 [USACO14FEB]路障Roadblock
    讨伐!数论
    网络流入门——EK算法
    最被低估的特质
    我的天哪我有博客了!
    Area POJ
  • 原文地址:https://www.cnblogs.com/xwqhl/p/10675307.html
Copyright © 2020-2023  润新知