Python中format的格式化转换:10个规则,轻松转换数据格式
发布时间: 2024-06-21 21:08:37 阅读量: 12 订阅数: 14 ![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_vip.0fdee7e1.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_vip.0fdee7e1.png)
![Python中format的格式化转换:10个规则,轻松转换数据格式](https://img-blog.csdnimg.cn/c2333d028e804cd78a464be55aa585de.png)
# 1. Python中的格式化转换基础
格式化转换是Python中一项强大的功能,它允许我们根据特定的格式规则将数据转换为字符串。格式化转换可以应用于各种数据类型,包括字符串、数字、日期和时间。
在Python中,格式化转换使用`format()`方法。`format()`方法接受一个格式字符串作为参数,格式字符串包含占位符,用于指定要插入到字符串中的数据。占位符可以是位置占位符或关键字占位符。位置占位符使用大括号`{}`表示,而关键字占位符使用关键字`{}`表示。
# 2. 格式化转换的语法规则
### 2.1 字符串格式化规则
#### 2.1.1 位置占位符
位置占位符使用百分号(%)后跟一个数字来指定要格式化的值的位置。例如:
```python
name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))
```
输出:
```
My name is John and I am 30 years old.
```
#### 2.1.2 关键字占位符
关键字占位符使用百分号(%)后跟一个变量名来指定要格式化的值。例如:
```python
name = "John"
age = 30
print("My name is %(name)s and I am %(age)d years old." % {"name": name, "age": age})
```
输出:
```
My name is John and I am 30 years old.
```
#### 2.1.3 对齐和填充
对齐和填充可以控制格式化后的值的输出格式。使用以下格式说明符:
- `-`: 左对齐
- `>`: 右对齐
- `^`: 居中对齐
- `=`: 在两侧填充
- `0`: 在左侧填充零
例如:
```python
name = "John"
age = 30
print("My name is %-10s and I am %03d years old." % (name, age))
```
输出:
```
My name is John and I am 030 years old.
```
### 2.2 数字格式化规则
#### 2.2.1 浮点数格式化
浮点数格式化使用以下格式说明符:
- `.`: 指定小数点后的位数
- `f`: 格式化为浮点数
- `e`: 格式化为科学计数法
例如:
```python
number = 123.456789
print("Formatted number: %.2f" % number)
print("Formatted number: %.3e" % number)
```
输出:
```
Formatted number: 123.46
Formatted number: 1.2345679e+02
```
#### 2.2.2 整数格式化
整数格式化使用以下格式说明符:
- `d`: 格式化为十进制整数
- `b`: 格式化为二进制整数
- `o`: 格式化为八进制整数
- `x`: 格式化为十六进制整数
例如:
```python
number = 123
print("Formatted number: %d" % number)
print("Formatted number: %b" % number)
print("Formatted number: %o" % number)
print("Formatted number: %x" % number)
```
输出:
```
Formatted number: 123
Formatted number: 1111011
Formatted number: 173
Formatted number: 7b
```
#### 2.2.3 二进制、八进制和十六进制格式化
二进制、八进制和十六进制格式化使用以下格式说明符:
- `0b`: 格式化为二进制整数
- `0o`: 格式化为八进制整数
- `0x`: 格式化为十六进制整数
例如:
```python
number = 123
print("Formatted number: %08b" % number)
print("Formatted number: %06o" % number)
print("Formatted number: %04x" % number)
```
输出:
```
Formatted number: 01111011
Formatted number: 000173
Formatted number: 007b
```
### 2.3 日期和时间格式化规则
#### 2.3.1 日期格式化
日期格式化使用以下格式说明符:
- `%Y`: 年份
- `%m`: 月份
- `%d`: 日期
- `%H`: 小时
- `%M`: 分钟
- `%S`: 秒
例如:
```python
import datetime
date = datetime.datetime(2023, 1, 1)
print("Formatted date: %Y-%m-%d" % date)
```
输出:
```
Formatted date: 2023-01-01
```
#### 2.3.2 时间格式化
时间格式化使用以下格式说明符:
- `%H`: 小时
- `%M`: 分钟
- `%S`: 秒
- `%f`: 微秒
例如:
```python
import datetime
time = datetime.datetime(2023, 1, 1, 12, 30, 15)
print("Formatted time: %H:%M:%S" % time)
```
输出:
```
Formatted time: 12:30:15
```
#### 2.3.3 日期时间组合格式化
日期和时间组合格式化使用以下格式说明符:
- `%Y-%m-%d %H:%M:%S
0
0
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)