博客简介
在Python中给字符串格式化有两个重要函数——str.format() && print( ), 大多数的 Python 代码仍然使用 % 操作符,随着Python向前发展,这种旧式的格式化最终会从该语言中移除, 而str.format()将会得到广发应用,在这节我们将会介绍二者的详细用法:
- 转化字符串str()和repr()方法
- format方法详解
- print方法详解
转化字符串str()和repr()方法
字符串在读入,输出时广泛应用。如果你想要将非字符串转(例如数字)化成字符串,那么有两种发法实现:
- str(): 函数返回一个用户易读的表达形式
- repr(): 产生一个解释器易读的表达形式,可以转义字符串中的特殊字符
- 举例:
#coding=UTF-8
print(str(1/7))
print(repr(1/7))
'''
0.14285714285714285
0.14285714285714285
'''
print(str("Hello world!"))
print(repr("Hello world!\n"))
'''
Hello world!
Hello world!
'Hello world!\n'
'''
for x in range(0,11):
print(repr(x).rjust(2),repr(x**2).rjust(3),repr(x**3).rjust(4))
for x in range(1, 11):
print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
'''
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
'''
print格式化
字符串格式化是Python程序员一个重要的技能,具有很大的实用性,格式转化规则如下:
- 使用方法:
%[(name)][flags][width].[precision]typecode
- 参数属性
参数 | 属性 | 是否必须 |
---|---|---|
(name) | 用于选择指定的key | 可选 |
flags | 对齐 | 可选 |
width | 字符串宽度 | 可选 |
.precision | 精度 | 可选 |
typecode | 格式化类型 | 必选 |
- flag对齐参数取值:
标志 |
含义 |
---|---|
|
值的转换将使用“替代形式”。 |
|
转换将为数字值填充零字符。 |
|
转换值将靠左对齐(如果同时给出 |
|
(空格) 符号位转换产生的正数(或空字符串)前将留出一个空格。 |
|
符号字符 ( |
- typecode格式化类型取值:
转换符 |
含义 |
注释 |
---|---|---|
|
有符号十进制整数。 |
|
|
有符号十进制整数。 |
|
|
有符号八进制数。 |
(1) |
|
过时类型 -- 等价于 |
(6) |
|
有符号十六进制数(小写)。 |
(2) |
|
有符号十六进制数(大写)。 |
(2) |
|
浮点指数格式(小写)。 |
(3) |
|
浮点指数格式(大写)。 |
(3) |
|
浮点十进制格式。 |
(3) |
|
浮点十进制格式。 |
(3) |
|
浮点格式。 如果指数小于 -4 或不小于精度则使用小写指数格式,否则使用十进制格式。 |
(4) |
|
浮点格式。 如果指数小于 -4 或不小于精度则使用大写指数格式,否则使用十进制格式。 |
(4) |
|
单个字符(接受整数或单个字符的字符串)。 |
|
|
字符串(使用 |
(5) |
|
字符串(使用 |
(5) |
|
字符串(使用 |
(5) |
|
不转换参数,在结果中输出一个 |
用法举例:
print("%c"%('C'))
print("%s"%('ABC'))
print("%c"%(65))
#print("%d"%('A'))
print("%u"%(123.5))
print("%o"%(12))
print("%X"%(123))
print("%f"%(1.223))
print("%.4f"%(1.223))
print("%e"%(1.223))
print("%#5d"%123)
print("% 5d"%123)
print("%+5d"%123)
print("%-5d"%123)
print("%05d"%123)
'''
C
ABC
A
123
14
7B
1.223000
1.2230
1.223000e+00
123
123
+123
123
00123
'''
format方法详解
自Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能,用法如下:
- 使用规则:
"{ :[name][flags][width].[precision]typecode },{}".format(value1,value2)
- { }和value对应规则1可以使用默认对应关系即 :之前不填参数
print("{} {}".format(11,22))
- { }和value对应规则2使用下标索引:
print("{0} {0} {1}".format(11,22))
- { }和value对应规则3使用关键字对应:
print("{a} {b} {c}".format(a='AA',b='BB',c='CC'))
- { }和value对应规则4如果format是一个单个的序列参数,而想要获得其中的某个value值,必须用用key或者索引指定
- 参数说明:
参数 | 属性 | 是否必须 |
---|---|---|
(name) | 用于选择指定的key | 可选 |
flags | 对齐 | 可选 |
width | 字符串宽度 | 可选 |
.precision | 精度 | 可选 |
typecode | 格式化类型 | 可选 |
- 参数取值:与旧规则不同的是,许多取值有所改动,比如说左右对齐:
<左对齐,>右对齐
数字 | 格式 | 输出 | 描述 |
---|---|---|---|
3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
3.1415926 | {:+.2f} | +3.14 | 带符号保留小数点后两位 |
-1 | {:+.2f} | -1.00 | 带符号保留小数点后两位 |
2.71828 | {:.0f} | 3 | 不带小数 |
5 | {:0>2d} | 05 | 数字补零 (填充左边, 宽度为2) |
5 | {:x<4d} | 5xxx | 数字补x (填充右边, 宽度为4) |
10 | {:x<4d} | 10xx | 数字补x (填充右边, 宽度为4) |
1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
0.25 | {:.2%} | 25.00% | 百分比格式 |
1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
13 | {:>10d} | 13 | 右对齐 (默认, 宽度为10) |
13 | {:<10d} | 13 | 左对齐 (宽度为10) |
13 | {:^10d} | 13 | 中间对齐 (宽度为10) |
11 | '{:b}'.format(11) '{:d}'.format(11) '{:o}'.format(11) '{:x}'.format(11) '{:#x}'.format(11) '{:#X}'.format(11) |
1011 11 13 b 0xb 0XB |
进制 |
- 用法举例:
print("{} {}".format(11,22))
#11 22
print("{0} {0} {1}".format(11,22))
#11 11 22
print("{a} {b} {c}".format(a='AA',b='BB',c='CC'))
#AA BB CC
print("{0[0]} {0[1]}".format(["A","B"]))
#A B
value=1.234
print("{1:.0f} {0:5f}".format(value,3.0))
#3 1.234000
value=1
print("{0:*<5}".format(value))
#1****
print("{0:0>5}".format(value))
#00001
print("{:,}".format(10000000000))
#10,000,000,000
print("{0:e}".format(12.30))
#1.230000e+01
print("{0:#b}".format(11))
#0b1011
print("{0:b}".format(11))
#1011
来源:CSDN
作者:飞翔的哈士奇
链接:https://blog.csdn.net/weixin_44307065/article/details/104578075