zoukankan      html  css  js  c++  java
  • Python学习笔记字符串操作之startswith()和endswith()方法,检查开始和结束位置

     随笔记录方便自己和同路人查阅。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      startswith()和endswith()方法返回True,如果它们所调用的字符串以该方法传入的字符串开始或结束。

    否则,方法返回False。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      1、startswith()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #被调用字符串开始位置是,startswith()方法传入的值
    startswith_name = 'hello world!'
    if startswith_name.startswith('hello'):
        print('True')
    else:
        print('False')
    
    #被调用字符串开始位置不是,startswith()方法传入的值
    startswith_name = 'hello world!'
    if startswith_name.startswith('world'):
        print('True')
    else:
        print('False')
    

      运行结果:

      2、endswith()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # # Autor: Li Rong Yang
    #被调用字符串结束位置是,endswith()方法传入的值
    startswith_name = 'hello world!'
    if startswith_name.endswith('world!'):
        print('True')
    else:
        print('False')
    
    #被调用字符串结束位置不是,endswith()方法传入的值
    startswith_name = 'hello world!'
    if startswith_name.endswith('hello'):
        print('True')
    else:
        print('False')
    

      运行结果:

  • 相关阅读:
    基于vue的可视化编辑器
    IOS系统兼容input keyup事件
    js滚动事件实现滚动触底加载
    移动端 input 输入框实现自带键盘“搜索“功能并修改X
    clipboard.js兼容ios
    js实现点击复制网页内容(基于clipboard.js)
    js实现点击复制网页内容(基于execCommand)
    knn 数字识别
    knn 算法 k个相近邻居
    sklearn 线性回归
  • 原文地址:https://www.cnblogs.com/lirongyang/p/9568289.html
Copyright © 2011-2022 走看看