Skip to Content
深入淺出程式設計
book

深入淺出程式設計

by David Griffiths, Paul Barry
August 2011
Intermediate to advanced
444 pages
8h 55m
Chinese
GoTop Information, Inc.
Content preview from 深入淺出程式設計
182 第六章
簡單又直接
展示字串,後面接著 4.000000e+ 0 0
"%s %e" % ("Value is", 16.0 ** 0.5)
為每一個字串格式找出對應的作用,我們已經先為你完成一個。
解答
"%7d" % (11232/3)
"%x" % 127
"%20s\n" % "Banana swirl"
"%s is $%4.2f" % ("Popsicle", 1.754)
"%s %f" % ("Value is", 16.0 ** 0.5)
"%07d" % (11232/3)
除字串外,也展示浮點數至兩位小數。
展示字串,後面接著 4.000000
展示值,以 0 補綴。
將數字展示為 16 進位。
將字串補綴成 20 個字元,接著展示換行字元。
展示計算結果,以空格補綴。
回想一下你需要為交易檔建立的紀錄。每一行必須以換行字
元作結束。如果你將信用卡號碼、銷售價格與描述存放在名
credit_card、price description 的變數裡,請寫下你會
使用什麼樣的字串格式︰
假如你只有一個值
要格式化,就不需
要在其前後加上圓
括號。
預設上,Python 會以
空格補綴。
16 進位的數字被用
在像是指定顏色之
類的地方。
\n 表示換行字元。
這表示使用 4 個字元。
這表示在小數點後面顯示
兩位數字。
值也可以是計算結果。
% 後面接著 0 表示「以 0 補綴」。
“%16s%07d%16s\n" % (credit_card, price*100, description)
信用卡號碼應該總是 ...
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

Kubernetes设计模式

Kubernetes设计模式

Bilgin Ibryam, Roland Huß
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9789862762691