Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0460
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---
| 名称 |
英文 |
作用 |
| 填充 |
fill |
空位填充符号 |
| 对齐 |
align |
左右对齐符号 |
| 符号 |
sign |
正负号 |
| 宽度 |
width |
输出宽度 |


- 原来的%(modulo)运算符可以按照不同进制来显示
- str.format可以吗??🤔





print("int: {0:d}; hex: {0:x}, oct: {0:o}, bin: {0:b}".format(42))
print("int: {0:d}; hex: {0:#x}, oct: {0:#o}, bin: {0:#b}".format(42))
print("{}".format(42))
print("{:d}".format(42))
print("{:b}".format(42))
print("{:o}".format(42))
print("{:x}".format(42))
print("{:X}".format(42))

print("{:#b}".format(42))
print("{:#o}".format(42))
print("{:#x}".format(42))
print("{:#X}".format(42))

width = 5
for num in range(17):
for base in 'dXob':
print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')
print()




print("{:_d}".format(2024))
print("{:#10_d}".format(2024))
print("{:#10,d}".format(2024))

- 其中10是宽度(width)
- 可以显示浮点型小数吗?

print("e===={:e}".format(3.14))
print("E===={:E}".format(3.14))
print("f===={:f}".format(3.14))
print("F===={:F}".format(3.14))
print("g===={:g}".format(3.14))
print("G===={:G}".format(3.14))
print("n===={:n}".format(3.14))
print("%===={:%}".format(3.14))
print("{}".format(3.14))

| 符号 |
来源 |
含义 |
| e |
exponent of 10 |
科学计数法 |
| E |
Exponent of 10 |
大写科学计数法 |
| f |
float |
浮点小数 |
| F |
Float |
大写浮点小数 |
| g |
general |
通用格式 |
| G |
General |
大写通用格式 |
| n |
number |
数字格式 |
| % |
percent |
百分比 |
| None |
|
默认格式 |




for align, text in zip("<^>", ["left", "center", "right"]):
print("{0:{fill}{align}16}".format(text, fill=align, align=align))






print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W'))
coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
print('Coordinates: {latitude}, {longitude}'.format(**coord))

print("{} and {}".format(3,8))
print("{0} and {0}".format(3,8))
print("{0[0]} and {0[1]}".format([3,8]))
print("{0[0]: {0[1]}}".format([3,8]))

- 这真的很有意思
- str.format还能干点什么?
print("{:08b}".format(ord("a")))

| 参数 |
含义 |
选项 |
| [[fill]align] |
[填充]对齐] |
<>=# |
| [sign] |
正负号 |
+-空 |
| ["#"] |
前缀 |
无 |
| [width] |
宽度 |
数值 |
| [grouping_option] |
千位分隔符 |
,_ |
| ["." precision] |
[.精度] |
数值 |
| [type] |
类型 |
bodxefgn |
- 我们还可以输出 数字的二进制形态
- 看看还有什么好玩的?🤔
- 下次再说👋
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。