zoukankan      html  css  js  c++  java
  • 04-Python里字符串的常用操作方法三-判断

    1、 startswith(): 判断字符串是否以某个子串开始,是则返回True,否则返回False

    示例:

    my_str = 'hello world and my and test and python'
    # 1、 startswith(): 判断字符串是否以某个子串开始,是则返回True,否则返回False
    print(my_str.startswith('hello'))  # True
    print(my_str.startswith('hel'))  # True
    print(my_str.startswith('hells'))  # False
    

    结果:

     2、endswith(): 判断字符串是否以某个子串结束,是则返回True, 否则返回False

    示例:

    my_str = 'hello world and my and test and python'
    # 2、endswith(): 判断字符串是否以某个子串结束,是则返回True, 否则返回False
    print(my_str.endswith('python'))    # True
    print(my_str.endswith('py'))        # False
    print(my_str.endswith('on'))        # True
    

    结果:

     3、isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False

    示例:

    # isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False
    my_str1 = 'my name python'
    my_str2 = 'python'
    print('isalpha():判断非空字符串是不是都是字母,是则返回True,否则返回False')
    print(my_str1.isalpha())    # False
    print(my_str2.isalpha())    # True
    

    结果:

     4、isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False

    示例:

    # isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False
    my_str1 = 'my name python'
    my_str3 = '123'
    print('isdigit():判断非空字符串是不是都是数字,是则返回True,否则返回False')
    print(my_str1.isdigit())    # False
    print(my_str3.isdigit())    # True
    

    结果:

     5、isalnum():判断非空字符串是不是数字或字母或数字与字母的组合

    示例:

    # isalnum():判断非空字符串是不是数字或字母或数字与字母的组合
    my_str1 = 'my name python'
    my_str2 = 'python'
    my_str3 = '123'
    my_str4 = '123abc'
    print('isalnum():判断非空字符串是不是数字或字母或数字与字母的组合')
    print(my_str1.isalnum())    # False
    print(my_str2.isalnum())    # True
    print(my_str3.isalnum())    # True
    print(my_str4.isalnum())    # True
    

    结果:

     6、isspace():判断字符串是不是空白

    示例:

    # isspace():判断字符串是不是空白
    my_str1 = 'my name python'
    my_str5 = ''
    my_str6 = '   '
    print('isspace():判断字符串是不是空白')
    print(my_str1.isspace())    # False
    print(my_str5.isspace())    # False
    print(my_str6.isspace())    # True
    

    结果:

     --------------------------------------------------------------------

    欢迎大家多多关注我的微信公众号,每日更新Python知识分享

  • 相关阅读:
    【一】、Cypress下载及安装介绍
    利用Selenium多用户同时开启多线程登录博客园
    实用的jsonpath模块
    Linux常用命令
    Python读取CSV文件
    python算法集锦【四】
    基础类封装-浏览器文件上载类库封装
    基础类封装-查找页面元素类库封装
    基础类封装-键盘类操作库封装
    自动化测试框架Python+selenium+unittest系列 之 配置文件读取(Python)
  • 原文地址:https://www.cnblogs.com/zack-dong/p/14069989.html
Copyright © 2011-2022 走看看