Python 3.1 快速導覽 - 字串型態的 zfill()

字串 (string) 型態 (type) 的 zfill() 方法 (method) ,回傳以 0 塞滿 width 的新字串

>
方法描述
str.zfill(width)回傳以 0 塞滿 width 的新字串


舉例示範如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
a = "Free your mind."
b = "Welcome to the desert... of the real."
c = "What is real? How do you define real?"
d = "There is no spoon."
 
print(a)
print(a.zfill(len(a) - 5))
print()
 
print(b)
print(b.zfill(len(b)))
print()
 
print(c)
print(c.zfill(len(c) + 5))
print()
 
print(d)
print(d.zfill(len(d) + 10))
print()
 
# 《程式語言教學誌》的範例程式
# 檔名:upper.py
# 功能:示範 Python 程式
# 作者:張凱慶


執行結果如下



中英文術語對照
字串string
型態type
方法method


內建型態




沒有留言: