type
Post
status
Published
date
Apr 14, 2026
slug
python003
summary
tags
python
category
python
icon
password
example-row

1. + 拼接

2. join() 方法(最推荐用于多个字符串)

3. format() 格式化

4. f-string(Python 3.6+ 推荐)

4.1 数字格式化

4.2 对齐和填充

4.3 各个部分说明

4.4 常用类型说明符

4.5 使用示例
运行结果
收入后面填充了6个空格,利润率后面填充了5个空格。

5. 百分号 (%) 格式化(旧式)

旧式写法,不推荐使用,更推荐现代的f-string格式。

6.多行字符串拼接

 
说明:
  1. %格式化是旧式方法,不推荐在新代码中使用。
  1. f-string 是最佳选择 (Python 3.6+),因为它:
      • 语法简洁
      • 可读性高
      • 性能好
      • 功能强大
  1. str.format()是过渡方案,适合 Python 3.0-3.5。
注意:
print("a", "b", "c") 看起来像拼接,但那是 print()函数的功能。逗号 (,) 不会拼接字符串,而是创建元组或分隔元素。
 
【python002】SQLAlchemy 2.0 异步增删改查用法【python004】@dataclass装饰器
Loading...