1. 程式人生 > 其它 >字串方法(二)

字串方法(二)

技術標籤:python基礎知識點手冊python

str.isalnum 是字母或數字?

字串方法 str.isalnum(),Python 官方文件描述如下:

help(str.isalnum)
Help on method_descriptor:

isalnum(self, /)
    Return True if the string is an alpha-numeric string, False otherwise.
    
    A string is alpha-numeric if all characters in the string are alpha-numeric and
    there is at least one character in the string.

如果字串中的所有字元都是字母或數字且至少有一個字元,則返回 True ,否則返回 False 。

''.isalnum()
False
'python123'.isalnum()
True
'python 123'.isalnum()
False
'γ'.isalnum()
True

str.isalpha 是字母(包括漢字等)?

字串方法 str.isalpha(),Python 官方文件描述如下:

help(str.isalpha)
Help on method_descriptor:

isalpha(self, /)
    Return True if the string is an alphabetic string, False otherwise.
    
    A string is alphabetic if all characters in the string are alphabetic and there
    is at least one character in the string.

如果字串中的所有字元都是字母,並且至少有一個字元,返回 True ,否則返回 False 。

字母字元是指那些在 Unicode 字元資料庫中定義為 ”Letter” 的字元,即那些具有 ”Lm”、”Lt”、”Lu”、”Ll” 或 ”Lo” 之一的通用類別屬性的字元。注意,這與 Unicode 標準中定義的 ”字母” 屬性不同。

此處的字母包括漢字等。

''.isalpha()
False
'γ'.isalpha()
True
'嗨你好'.isalpha()
True
'嗨!你好'.isalpha()
False

str.isdecimal 是十進位制字元?

字串方法 str.isdecimal(),Python 官方文件描述如下:

help(str.isdecimal)
Help on method_descriptor:

isdecimal(self, /)
    Return True if the string is a decimal string, False otherwise.
    
    A string is a decimal string if all characters in the string are decimal and
    there is at least one character in the string.

如果字串中的所有字元都是十進位制字元且該字串至少有一個字元,則返回 True,否則返回 False。

十進位制字元指那些可以用來組成 10 進位制數字的字元。嚴格地講,十進位制字元是 Unicode 通用類別 ”Nd” 中的一個字元。

''.isdecimal()
False
'3.14'.isdecimal()
False
'0123'.isdecimal()
True
'5²'.isdecimal()
False
'python'.isdecimal()
False
b'100'.isdecimal()
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-13-52e1682babfd> in <module>
----> 1 b'100'.isdecimal()


AttributeError: 'bytes' object has no attribute 'isdecimal'

str.isdigit 是數字?

字串方法 str.isdigit(),Python 官方文件描述如下:

help(str.isdigit)
Help on method_descriptor:

isdigit(self, /)
    Return True if the string is a digit string, False otherwise.
    
    A string is a digit string if all characters in the string are digits and there
    is at least one character in the string.

如果字串中的所有字元都是數字,並且至少有一個字元,返回 True ,否則返回 False 。

數字包括十進位制字元和需要特殊處理的數字,如相容性上標數字。這包括了不能用來組成 10 進位制數的數字,如 Kharosthi 數。嚴格地講,數字是指屬性值為 Numeric_Type=Digit 或 Numeric_Type=Decimal 的字元。

'一'.isdigit()
False
'3.14'.isdigit()
False
'123'.isdigit()
True
b'123'.isdigit()
True
'5²'.isdigit()
True

str.isnumeric 是數值字元?

字串方法 str.isnumeric(),Python 官方文件描述如下:

help(str.isnumeric)
Help on method_descriptor:

isnumeric(self, /)
    Return True if the string is a numeric string, False otherwise.
    
    A string is numeric if all characters in the string are numeric and there is at
    least one character in the string.

如果字串中至少有一個字元且所有字元均為數值字元則返回 True,否則返回 False。

數值字元包括數字字元,以及所有在 Unicode 中設定了數值特性屬性的字元,例如 U+2155, VUL-GAR FRACTION ONE FIFTH。正式的定義為:數值字元就是具有特徵屬性值 Numeric_Type=Digit, Numeric_Type=Decimal 或 Numeric_Type=Numeric 的字元。

此處所指數字包括羅馬數字,漢字數字等。

'②'.isnumeric()
True
'3.14'.isnumeric()
False
'5²'.isnumeric()
True
'Ⅷ'.isnumeric()
True
'一'.isnumeric()
True
'壹'.isnumeric()
True

str.islower 是小寫?

字串方法 str.islower(),Python 官方文件描述如下:

help(str.islower)
Help on method_descriptor:

islower(self, /)
    Return True if the string is a lowercase string, False otherwise.
    
    A string is lowercase if all cased characters in the string are lowercase and
    there is at least one cased character in the string.

如果字串中至少有一個區分大小寫的字元且此類字元均為小寫則返回 True,否則返回 False。

'嗨'.islower()
False
'嗨 Abc'.islower()
False
'嗨 abc'.islower()
True

str.isupper 是大寫?

字串方法 str.isupper(),Python 官方文件描述如下:

help(str.isupper)
Help on method_descriptor:

isupper(self, /)
    Return True if the string is an uppercase string, False otherwise.
    
    A string is uppercase if all cased characters in the string are uppercase and
    there is at least one cased character in the string.

如果字串中至少有一個區分大小寫的字元且此類字元均為大寫則返回 True,否則返回 False。

'Γ'.isupper()
True
'嗨 AB'.isupper()
True
'嗨 Ab'.isupper()
False

str.istitle 是標題字串?

字串方法 str.istitle(),Python 官方文件描述如下:

help(str.istitle)
Help on method_descriptor:

istitle(self, /)
    Return True if the string is a title-cased string, False otherwise.
    
    In a title-cased string, upper- and title-case characters may only
    follow uncased characters and lowercase characters only cased ones.

如果字串中至少有一個字元且為標題字串則返回 True,例如大寫字元之後只能帶非大寫字元而小寫字元必須有大寫字元打頭。否則返回 False。

'Abc Py'.istitle()
True
'嗨 A11'.istitle()
True
'嗨 Abc'.istitle()
True
'嗨 ABC'.istitle()
False