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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival

Publisher Resources

ISBN: 9789864766819