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')

  運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM