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

  • 相关阅读:
    Iframe 自适应高度并实时监控高度变化的js代码
    asp.net下载文件几种方式
    C# 使用ffmpeg视频截图
    C# 读取Excel和DBF文件
    C# 多线程下载
    C# http get与post请求方法
    socket实例
    C# 汉字转为拼音
    读取Excel中数据
    【BZOJ2238】Mst 最小生成树+LCA+堆
  • 原文地址:https://www.cnblogs.com/zack-dong/p/14069989.html
Copyright © 2011-2022 走看看