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知识分享

  • 相关阅读:
    xml转义字符在mybatis动态sql中的使用
    jdbc类型与java类型
    aop日志(记录方法调用日志)
    mysql数据库关联查询【lert join】常见使用
    maven项目基本配置
    mapper文件的参数传入与获取
    idea新建项目出现push rejected如何解决
    快速从2个List集合中找出相同/不同元素
    Windows 环境下安装RocketMQ
    RabbitMQ java客户端集成 Spring 开发环境
  • 原文地址:https://www.cnblogs.com/zack-dong/p/14069989.html
Copyright © 2011-2022 走看看