string --- 常見的字串操作¶
字串常數¶
此模組中定義的常數為:
- string.ascii_letters¶
下文描述的
ascii_lowercase和ascii_uppercase常數的串接,該值不依賴於區域設定。
- string.ascii_lowercase¶
小寫字母
'abcdefghijklmnopqrstuvwxyz'。該值與地區設定無關且不會改變。
- string.ascii_uppercase¶
大寫字母
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'。此值與地區設定無關且不會改變。
- string.digits¶
字串
'0123456789'。
- string.hexdigits¶
字串
'0123456789abcdefABCDEF'。
- string.octdigits¶
字串
'01234567'。
- string.punctuation¶
在
C語言中被視為標點符號的 ASCII 字元的字串:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~。
- string.printable¶
ASCII 字元的字串被 Python 是為可被列印輸出的。這是
digits、ascii_letters、punctuation和whitespace的組合。備註
By design,
string.printable.isprintable()returnsFalse. In particular,string.printableis not printable in the POSIX sense (see LC_CTYPE).
- string.whitespace¶
包含所有 ASCII 字元的字串都視為空白字元 (whitespace)。 包含空格 (space)、製表符號 (tab)、換行符號 (linefeed)、return、換頁符號 (formfeed) 和垂直製表符號 (vertical tab) 這些字元。
自訂字串格式¶
透過 PEP 3101 中描述的 format() 方法,內建字串類別提供了進行複雜變數替換和數值格式化的能力。string 模組中的 Formatter 類別模組可讓你使用與內建 format() 方法相同的實作來建立和自訂你自己的字串格式化行為。
- class string.Formatter¶
Formatter類別有以下的公開方法:- format(format_string, /, *args, **kwargs)¶
主要的 API 方法。它接收一個格式字串及一組任意的位置引數與關鍵字引數,是呼叫
vformat()的包裝器 (wrapper)。在 3.7 版的變更: 現在格式字串引數是僅限位置參數。
- vformat(format_string, args, kwargs)¶
This function does the actual work of formatting. It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the
*argsand**kwargssyntax.vformat()does the work of breaking up the format string into character data and replacement fields. It calls the various methods described below.
In addition, the
Formatterdefines a number of methods that are intended to be replaced by subclasses:- parse(format_string)¶
將 format_string 放入迴圈,並回傳一個可疊代物件,其元素為 (literal_text, field_name, format_spec, conversion)。這會被
vformat()用於將字串裁切為字面文本或替換欄位。The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string. If there is no replacement field, then the values of field_name, format_spec and conversion will be
None. The value of field_name is unmodified and auto-numbering of non-numbered positional fields is done byvformat().
- get_field(field_name, args, kwargs)¶
Given field_name, convert it to an object to be formatted. Auto-numbering of field_name returned from
parse()is done byvformat()before calling this method. Returns a tuple (obj, used_key). The default version takes strings of the form defined in PEP 3101, such as "0[name]" or "label.title". args and kwargs are as passed in tovformat(). The return value used_key has the same meaning as the key parameter toget_value().
- get_value(key, args, kwargs)¶
Retrieve a given field value. The key argument will be either an integer or a string. If it is an integer, it represents the index of the positional argument in args; if it is a string, then it represents a named argument in kwargs.
The args parameter is set to the list of positional arguments to
vformat(), and the