Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
284
|
8 章 字串類的東西
以一個格式化運算式被記錄(logged)的是
format
,其中每個格式指定符
都被
values
的對應項目依據指定符轉換為字串後的結果所取代。這裡有一
些簡單的例子:
import logging
logging.getLogger().setLevel(logging.INFO)
x = 42
y = 3.14
z = 'george'
logging.info('result = %d', x) # 紀錄: result = 42
logging.info('answers: %d %f', x, y) # 紀錄: answers: 42 3.140000
logging.info('hello %s', z) # 紀錄: hello george
格式指定符的語法
一個格式指定符可以包括修飾詞(modifiers)來控制
values
中對應的項目
如何轉換為一個字串。一個格式指定符的組成部分,依序是:
1. 必要的前導字元
%
,標示指定符的開頭
2. 零或多個選擇性的轉換旗標(conversion flags):
#
轉換使用一個替代形式(如果該型別有的話)。
0
轉換會以零填補。
-
轉換是向左對齊的。
一個空格
正數的前面會放有一個空格。
+
一個數值符號(
+
-
)放在任何的數值轉換之前。
3. 轉換選擇性的最小寬度:一或多個數字,或一個星號(
*
),代表寬度
取自
values
中的下一個項目
4. 用於轉換的一個選擇性的精確度(precision):一個點號(
.
)後面接著
零或多個數字,或者一個 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819