文字与字串 ( 格式化 )
在 Python 里,除了可以使用基本功能串接字串,也可以针对不同的格式,将数据插入字串当中,由于 Python 版本的不同,这篇教学会介绍三种格式化字串的方法,透过字串的格式化,也可以轻松做出数字“补零”的效果。
本篇使用的 Python 版本为 3.7.12,所有范例可使用 Google Colab 实作,不用安装任何软件 ( 参考:使用 Google Colab )
%
“%”的字串格式化是“旧式”的格式化方法,适用于 Python 2 和 3 的版本,操作方式为“格式化字串 % 資料”,输出结果会将数据插入格式化字串的位置。
| 格式化字串 | 转换型态 |
|---|---|
| %s | 字串 |
| %d | 十进制整数 |
| %x | 十六进制整数 |
| %o | 八进制整数 |
| %b | 二进制整数 |
| %f | 十进制浮点数 |
| %e | 指数浮点数 |
| %g | 十进制或指数浮点数 |
| %% | 常值 % |
下面的例子,印出 a 的内容时,会将内容的 %s 替换成 % 后方的字串,%d 会将浮点数的小数点无条件舍去,%f 则会将整数转换成浮点数。( 如果用 %s 显示数字,数字的型态会被转换成文字 )
a = 'Hello world, I am %s!!'
b = 'there are %d dollars'
c = 'there are %f dollars'
print(a % 'oxxo') # Hello world, I am oxxo!!
print(a % 'xoox') # Hello world, I am xoox!!
print(b % 2.5) # there are 2 dollars ( 小數點被無條件捨去 )
print(c % 2) # there are 2.000000 dollars ( 整數轉換成浮點數 )
格式化字串的 % 和形态代号间,可以加入其他数值,来指定最小宽度、最大字元、对齐与精确度:
| 格式化数值 | 说明 |
|---|---|
| 不加东西、+ | 靠右对齐 |
| - | 靠左对齐 |
| 数字 | 最小宽度 ( 如果字串超过最小宽度,以字串的宽度为主 ) |
| 数字.数字 | 最小宽度.最大字元数,如果后方是 f (%f),第二个数字表示小数点位数 |
下面的例子可以看到 %12s 会在 hello 前方加上七个空格 ( 7 + hello 总共 12 个字元 ),%.3s 会让 hello 只剩下 hel ( 3 个字元 ),%.3f 会让 123.456789 只留下小数点三位。
print('%s world' % 'hello') # hello world
print('%12s world' % 'hello') # hello world
print('%+12s world' % 'hello') # hello world
print('%-12s world' % 'hello') # hello world
print('%.3s world' % 'hello') # hel world
print('%12.3s world' % 'hello') # hel world
print('%.3f world' % 123.456789) # 123.457 world
如果有多个数值需要格式化,可以在 % 后方用小括号包住对应的参数,参数的数量和顺序必须和前方的格式化字元相同。
a = '%s world, ther are %f dollars!'
b = a % ('hello', 2.5)
print(b) # hello world, ther are 2.500000 dollars!
.format
“{}”和“foramt()”是 Python 3 所使用的“新式”格式化,操作方式为“格式化字串.format(資料)”,输出结果会将数据插入格式化字串的位置,下面的例子,会将 world 和 oxxo 两个字串,分别插入字串中的两个 {}。
a = 'hello {}, I am {}'
b = a.format('world', 'oxxo')
print(b) # hello world, I am oxxo
{} 里可以填入数字,数字表示“填入数据的顺序”,如果将上面程序里的 {} 加入数字,就会呈现不同的结果。
a = 'hello {1}, I am {0}'
b = a.format('world', 'oxxo')
print(b) # hello oxxo, I am world ( world 和 oxxo 互換了 )
{} 里可以填入具名引数,如果将上面程序里的 {} 加入名称,就会放入指定名称的内容。
a = 'hello {m}, I am {n}'
b = a.format(m='world', n='oxxo')
print(b) # hello world, I am oxxo
{} 里可以填入字典的引数,如果将上面程序里的 {} 加入对应的引数,就会放入指定的字典内容。
a = 'hello {0[x][m]}, I am {0[y][m]}'
b = {'x': {'m':'world', 'n':'oxxo'}, 'y':{'m':'QQ', 'n':'YY'}}
c = a.format(b)
print(c) # hello world, I am QQ
新式的格式化字串和 % 定义略有不同,可以加入其他数值,来指定最小宽度、最大字元、对齐与精确度:
| 格式化数值 | 说明 |
|---|---|
| : | 开始需要加上冒号 |
| 不加东西、> | 靠右对齐 |
| < | 靠左对齐 |
| ^ | 置中对齐 |
| 填补字元 | 将不足最小宽度的空白,填满指定字元 |
| 数字.数字 | 最小宽度.最大字元数,如果后方是 f (%f),第二个数字表示小数点位数 |
数据的型态也由 % 改为“:”表示。
| 格式化字串 | 转换型态 |
|---|---|
| :s | 字串 |
| :d | 十进制整数 |
| :x | 十六进制整数 |
| :o | 八进制整数 |
| :b | 二进制整数 |
| :f | 十进制浮点数 |
| :e | 指数浮点数 |
| :g | 十进制或指数浮点数 |
下面的例子可以看到 {:-^10s} 会将 world 置中对齐,并将不足最小宽度的部分补上 - 的符号,{:^10.3f} 会让 123.456789 只留下小数点三位。
a = 'hello {}, I am {}'.format('world','oxxo')
b = 'hello {:10s}, I am {:10s}'.format('world','oxxo')
c = 'hello {:>10s}, I am {:>10s}'.format('world','oxxo')
d = 'hello {:-^10s}, I am {:+^10s}'.format('world','oxxo')
e = 'hello {:-^10.3s}, I am {:-^10s}'.format('world','oxxo')
f = 'hello {:-^10.3s}, I am {:^10.3f}'.format('world',123.456789)
print(a) # hello world, I am oxxo
print(b) # hello world , I am oxxo
print(c) # hello world, I am oxxo
print(d) # hello --world---, I am +++oxxo+++
print(e) # hello ---wor----, I am ---oxxo---
print(f) # hello ---wor----, I am 123.457
f-string
f-string 是 Python 3.6 加入的字串格式化功能,也是现在比较推荐的格式化方法,操作方式为“f{變數名稱或運算式}”( 开头可以使用 f 或 F ),输出结果会将变数或运算式的内容,放入指定的位置,下方的程序执行后,会将变数 a 和 b 的内容,放入 c 的字串里。
a = 'world'
b = 'oxxo'
c = f'hello {a}, I am {b}'
print(c) # hello world, I am oxxo
f-string 的格式化字串的定义和 .format 类似,可以加入其他数值,来指定最小宽度、最大字元、对齐与精确度。下面的例子可以看到 {b:+^10} 会将 oxxo 置中对齐,并将不足最小宽度的部分补上 + 的符号,{a:-<10.3} 会将 world 靠左对齐,并指取出 wor 三个字元,空白的部分补上 - 的符号。
a = 'world'
b = 'oxxo'
c = f'hello {a:<10s}, I am {b:>10s}'
d = f'hello {a:-<10s}, I am {b:+^10s}'
e = f'hello {a:-<10.3s}, I am {b:+^10.2s}'
f = f'hello {a.upper()}, I am {b.title()}'
print(c) # hello world , I am oxxo
print(d) # hello world-----, I am +++oxxo+++
print(e) # hello wor-------, I am ++++ox++++
print(f) # hello WORLD, I am Oxxo
了解原理后,就可以透过字串格式化的方式,实作“补零”的效果。
for i in range(1,101):
print(f'{i:03d}',end=' , ')
'''
001 , 002 , 003 , 004 , 005 , 006 , 007 , 008 , 009 , 010 ,
011 , 012 , 013 , 014 , 015 , 016 , 017 , 018 , 019 , 020 ,
021 , 022 , 023 , 024 , 025 , 026 , 027 , 028 , 029 , 030 ,
031 , 032 , 033 , 034 , 035 , 036 , 037 , 038 , 039 , 040 ,
041 , 042 , 043 , 044 , 045 , 046 , 047 , 048 , 049 , 050 ,
051 , 052 , 053 , 054 , 055 , 056 , 057 , 058 , 059 , 060 ,
061 , 062 , 063 , 064 , 065 , 066 , 067 , 068 , 069 , 070 ,
071 , 072 , 073 , 074 , 075 , 076 , 077 , 078 , 079 , 080 ,
081 , 082 , 083 , 084 , 085 , 086 , 087 , 088 , 089 , 090 ,
091 , 092 , 093 , 094 , 095 , 096 , 097 , 098 , 099 , 100 ,
'''
微信扫码关注
抖音扫码关注