zoukankan      html  css  js  c++  java
  • Python中的startswith和endswith函数使用实例

    Python中的startswith和endswith函数使用实例

    Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数判断文本是否以某个字符开始,endswith()函数判断文本是否以某个字符结束。

    startswith()函数

    此函数判断一个文本是否以某个或几个字符开始,结果以True或者False返回。

    代码如下:
    text='welcome to qttc blog'
    print text.startswith('w')      # True
    print text.startswith('wel')    # True
    print text.startswith('c')      # False
    print text.startswith('')       # True

    endswith()函数

    此函数判断一个文本是否以某个或几个字符结束,结果以True或者False返回。

    代码如下:
    text='welcome to qttc blog'
    print text.endswith('g')        # True
    print text.endswith('go')       # False
    print text.endswith('og')       # True
    print text.endswith('')         # True
    print text.endswith('g ')       # False

    判断文件是否为exe执行文件

    我们可以利用endswith()函数判断文件名的是不是以.exe后缀结尾判断是否为可执行文件

    代码如下:
    # coding=utf8
     
    fileName1='qttc.exe'
    if(fileName1.endswith('.exe')):
        print '这是一个exe执行文件'  
    else:
        print '这不是一个exe执行文件'
     
    # 执行结果:这是一个exe执行文件

    判断文件名后缀是否为图片

    代码如下:

    # coding=utf8
     
    fileName1='pic.jpg'
    if fileName1.endswith('.gif') or fileName1.endswith('.jpg') or fileName1.endswith('.png'):
        print '这是一张图片'
    else:
        print '这不是一张图片'
        
    # 执行结果:这是一张图片

  • 相关阅读:
    AirtestIDE基本功能(二)
    AirtestIDE基本功能(一)
    Pycharm Debug功能详解
    AirtestIDE环境安装
    leetcode-338. 比特位计数
    leetcode-401. 二进制手表
    leetcode-392. 判断子序列
    leetcode-155. 最小栈
    leetcode-111. 二叉树的最小深度
    leetcode-110. 平衡二叉树
  • 原文地址:https://www.cnblogs.com/amengduo/p/9586726.html
Copyright © 2011-2022 走看看