字符串格式化自2.6以后python提供更强大的函数str.format()
# 语法
用{}代替%
## EXP 1:简单例子,关键字映射
```python
x = 1
y = "shabi"
z = "wo shi {op1},{op2}".format(op1=y,op2=x)
print z
```
## EXP 2:转义{}
```python
x = 1
y = "shabi"
z = "wo shi {op}{{shit}}".format(op=y)
print z
```
## EXP3: 位置映射
```python
x = 1
y = "shabi"
z = "wo shi {0},{1},{0}".format(y,x)
print z
```
## EXP4: 列表(list,tuple)下表映射
```python
p = [‘shabi‘,1]
pp = [‘jiba‘,‘wanyi‘]
z = "wo shi {0[0]},{0[1]},{0[0]},{1[1]}".format(p,pp)
print z
```
## EXP5: 通过类属性?
```python
```
## EXP6: 用于进制转换
```python
print ‘{:b}‘.format(17)
print ‘{:o}‘.format(17)
print ‘{:d}‘.format(17)
print ‘{:x}‘.format(17)
```
时间: 2024-11-05 12:24:23