zoukankan      html  css  js  c++  java
  • Python学习笔记字符串操作之isalpha()、isalnum()、isdecimal()、isspace()和istitle()方法

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

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

      除了islower()和isupper()方法外,还有几个isX的字符串方法,这些方法返回一个布尔值,描述了字符串

    的特点。

      isalpha()返回True,如果字符串只包含字母,并且非空

      isalnum()返回True,如果字符串只包含字母和数字,并且非空

      isdecimal()返回True,如果字符串只包含数字字符,并且非空

      isspace()返回True,如果字符串只包含空格、制表符和换行,并且非空

      istitle()返回True,如果字符串仅包含以大写字母开头、后面都是小写字母的单词

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

      1、isalpha()方法,示例代码

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #isalpha方法,包含大小写字母
    str_name = 'HELLOword'
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和空格
    str_name = 'HELLO word'
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和特殊字符
    str_name = 'HELLOword!'
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和数字
    str_name = 'HELLOword123'
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    
    # isalpha方法,空字符串
    str_name = ''
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    

      运行结果:

      2、isalnum()方法,示例代码

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #isalpha方法,只包含大小写字母
    str_name = 'HELLOword'
    if str_name.isalnum():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含数字
    str_name = '1234'
    if str_name.isalnum():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和数字
    str_name = 'HELLOword123'
    if str_name.isalnum():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和空格
    str_name = 'HELLO word'
    if str_name.isalnum():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和特殊字符
    str_name = 'HELLOword!'
    if str_name.isalpha():
        print('True')
    else:
        print('False')
    
    # isalpha方法,空字符串
    str_name = ''
    if str_name.isalnum():
        print('True')
    else:
        print('False')
    

      运行结果:

      3、isdecimal()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #isalpha方法,只包含大小写字母
    str_name = 'HELLOword'
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含数字
    str_name = '1234'
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和数字
    str_name = 'HELLOword123'
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和空格
    str_name = 'HELLO word'
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和特殊字符
    str_name = 'HELLOword!'
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    
    # isalpha方法,空字符串
    str_name = ''
    if str_name.isdecimal():
        print('True')
    else:
        print('False')
    

      运行结果:

      4、isspace()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #isalpha方法,只包含大小写字母
    str_name = 'HELLOword'
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含数字
    str_name = '1234'
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和数字
    str_name = 'HELLOword123'
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和空格
    str_name = 'HELLO word'
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和特殊字符
    str_name = 'HELLOword!'
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,空字符串
    str_name = ''
    if str_name.isspace():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含空格、制表符
    str_name = '     '
    if str_name.isspace():
        print('True')
    else:
        print('False')
    

      运行结果:

      5、istitle()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    #isalpha方法,只包含大小写字母
    str_name = 'HELLOword'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含数字
    str_name = '1234'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和数字
    str_name = 'HELLOword123'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和空格
    str_name = 'HELLO word'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,包含大小写字母和特殊字符
    str_name = 'HELLOword!'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,空字符串
    str_name = ''
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含空格、制表符
    str_name = '     '
    if str_name.istitle():
        print('True')
    else:
        print('False')
    
    # isalpha方法,只包含第一个字符为大写且只包含字母的字符串
    str_name = 'Helloword'
    if str_name.istitle():
        print('True')
    else:
        print('False')
    

      运行结果:

  • 相关阅读:
    【腾讯Bugly干货分享】微信Tinker的一切都在这里,包括源码(一)
    【腾讯Bugly干货分享】iOS10 SiriKit QQ适配详解
    【腾讯Bugly干货分享】安卓单元测试:What, Why and How
    【腾讯Bugly干货分享】Android Linker 与 SO 加壳技术
    【腾讯优测干货分享】Android内存泄漏的简单检查与分析方法
    【腾讯Bugly经验分享】程序员的成长离不开哪些软技能?
    【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验
    从零开始安装Hadoop视频教程
    如何在MAC机器中实现移动设备WiFI上网(没有专门的无线路由器的情况)
    Alfresco安装与配置图解
  • 原文地址:https://www.cnblogs.com/lirongyang/p/9568222.html
Copyright © 2011-2022 走看看