← Back to list

TQC Python(1):字串與數值格式化輸出

最近被公司要求報考 TQC 證照,沒有我會的 JavaScript,所以只好報 Python 3 基礎程式語言 (考科代碼 SPY3)。接下來我將根據 SPY3 的題型範圍著手學習 Python,目的僅是為了考到證照,不是從事開發。我將以網誌的方式撰寫一連串的學習筆記。

Roan · 2024-11-17 04:02 · 0 claps · 6.1 min read
#pythoc #tqc #spy3 #python-programming #python3
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

TQC Python(1):字串與數值格式化輸出

最近被公司要求報考 TQC 證照,沒有我會的 JavaScript,所以只好報 Python 3 基礎程式語言 (考科代碼 SPY3)。接下來我將根據 SPY3 的題型範圍著手學習 Python,目的僅是為了考到證照,不是從事開發。我將以網誌的方式撰寫一連串的學習筆記。

4K photo of a software engineer taming and training a large python in an office. Behind them is a white board, written “SPY3” — ar 3:2 — v 6.1

4K photo of a software engineer taming and training a large python in an office. Behind them is a white board, written “SPY3” — ar 3:2 — v 6.1

目錄

- 術科範圍
- 字串與整數數值格式化輸出(format 寫法)
- 字串與整數數值格式化輸出(f-string 寫法)
- 整數數值格式化輸出例題
- 浮點數格式化輸出
- 浮點數格式化輸出例題

術科範圍

從考古題來看,至少(但不限於)要會以下範圍:

  • 基本認識
  • 基本程式設計
  • 選擇敘述
  • 迴圈敘述與不定數迴圈
  • 函式
  • 陣列
  • 字串與檔案處理
  • 格式化輸出
  • 倍數判斷
  • 迴圈偶數連加
  • 乘積
  • 進階控制流程

一、字串與整數數值格式化輸出

1. format 寫法

先看範例。

exa = 'Good {}. This is a {}. I am {}.'.format("morning","pen", "fine")
print(exa) #Good morning. This is a pen. I am fine.

exb='Good {:1s}. This is a {:<5s}. I am {:^10}, thank {:>5}.'.format("morning","pen","fine","you")
print(exb) #Good morning. This is a pen  . I am    fine   , thank   you.

{} 與 format() 是 Python 3 使用的格式化,寫法為

格式化字串.format(資料)

.format 裡面的資料會依據填入字串的{} 裡。參考範例 exa。

  1. {} 可自訂格式化數值。首先要加:(冒號),再依序加對齊方向、填滿字元、轉換型態。

  2. 例如 {:<10s} 意思就是:{}內的資料靠左,若未滿10字元其餘就用空格填滿,輸入的資料必須為字串。

  3. 範例 exb 中,{:1s} 要填滿1字元,但因為資料 “morning” 長度已超過1字元,所以沒影響。

  4. {:<5s} 代表靠左,要填滿5字元,所以 pen(3字元)後面多了2空格總共5字元。 ^ 與 > 分別代表置中、靠左。

  5. 如果沒加 s,例如範例 exb 的 {:>5},表示會把輸入的資料自動轉換成字串。如果有加 s,代表輸入的資料型別必須為字串,否則就會出錯;但未來若實際開發建議要加,以便偵錯。上例可以把資料先行改為字串。例如:

print('{:s}'.format(123)) #因為 123 型別是數值不是字串,所以出錯。
print('{:s}'.format('123')) #輸出字串
print('{:s}'.format(str(123))) #把數值先行轉為字串

photo of a charming Taiwanese girl working on a computer in her room, with her pet python accopanying her on the side. — ar 3:2 — s 0 — v 6.1

photo of a charming Taiwanese girl working on a computer in her room, with her pet python accopanying her on the side. — ar 3:2 — s 0 — v 6.1

2. f-string 寫法

另一種個人認為更好看的寫法:

f{變數名稱或運算式}

除了寫法有點不同外,其餘規格一樣,所以只要看懂 format 相信就能看懂 f-string。接下來都會以 f-string 呈現。

整數數值格式化輸出例題

【例題】
輸入:
1
12
123
1234
輸入:
|    1    12|
|  123  1234|
|1     12   |
|123   1234 |

【解答】
#format 寫法
a=input()
b=input()
c=input()
d=input()

print('|{:>5s} {:>5s}|'.format(a,b))
print('|{:>5s} {:>5s}|'.format(c,d))
print('|{:<5s} {:<5s}|'.format(a,b))
print('|{:<5s} {:<5s}|'.format(c,d))

#f-string 寫法
a=input()
b=input()
c=input()
d=input()

print(f'|{a:>5s} {b:>5s}|')
print(f'|{c:>5s} {d:>5s}|')
print(f'|{a:<5s} {b:<5s}|')
print(f'|{c:<5s} {d:<5s}|')

二、浮點數格式化輸出

浮點數(float)就是有小數點的數字,是另一種型別。繼之前的整數格式化輸出,了解浮點格式化輸出就更快了。

以下以 f-string 為例:

f'{資料:.指定位數f}'

加上 :f 就是十進制的浮點數,意思是就是:輸出資料為小數點指定位數的浮點數。輸入資料的小數點會四捨五入至指定的位數。

a=123.456
print(f'{a}') #123.456
print(f'{a:f}') #123.456000;沒有指定小數點位數,預設是小數點六位

print(f'{a:.2f}') #123.46;四捨五入到小數點兩位

要注意的是,input() 輸入的型別是字串。因此要先將輸入轉為浮點數,避免出錯:

a=float(input())

浮點數格式化輸出例題

【題目一】
輸入:
1.2
12.34
123.456
1234.5678
輸出:
|   1.20   12.34|
| 123.46 1234.57|
|1.20    12.34  |
|123.46  1234.57|

【解答】
a = float(input())
b = float(input())
c = float(input())
d = float(input())

print(f'|{a:>7.2f} {b:>7.2f}|')
print(f'|{c:>7.2f} {d:>7.2f}|')
print(f'|{a:<7.2f} {b:<7.2f}|')
print(f'|{c:<7.2f} {d:<7.2f}|')

【題目二】
請撰寫一程式,讓使用者輸入要購買的商品數量。
商品單價 23.34 元,
計算總共要花多少錢並輸出至小數點後第二位。舉例:
輸入:
5
輸出:
116.70

【解答】
x=float(input()) #若沒有先轉為浮點數,因為型別預設是字串,與 23.34 型別不同,會出錯
print(f'{x*23.34:.2f}')

a Taiwanese female engineer holding a laptop and her pet python in an office, feeling accomplished and joyful — ar 3:2 — s 25 — v 6.1

a Taiwanese female engineer holding a laptop and her pet python in an office, feeling accomplished and joyful — ar 3:2 — s 25 — v 6.1


메타데이터
post_id
2f37c0d1e5e4
slug
tqc-python-2f37c0d1e5e4
url
https://medium.com/@roan6903/tqc-python-2f37c0d1e5e4
canonical_url
https://medium.com/@roan6903/tqc-python-2f37c0d1e5e4
author_url
https://medium.com/@roan6903
status
ok
fetched_at
2026-07-22 03:37:30