python 格式输出(%用法和format)

今天修改程序,比较纠结用哪个,搜资料整理一下。

参考网页:python基础_格式化输出(%用法和format用法)

format()用法相对于基本格式%的用法,功能要强大很多。将字符串当成模板,通过传入的参数进行格式化,并且使用大括号{}作为特殊字符代替%

1.%用法

(1)整数输出   %o -八进制; %d-十进制;  %x-16进制

#correct
print("the number is %d"%20)
输出:the number is 20

#error
print("the number is %d",20)
输出:the number is %d 20

(2)浮点数输出

格式化输出

%f ——保留小数点后面六位有效数字,
  %.3f,保留3位小数位
%e ——保留小数点后面六位有效数字,指数形式输出
  %.3e,保留3位小数位,使用科学计数法
%g ——在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
  %.3g,保留3位有效数字,使用小数或科学计数法

 print('%.2g' % 1111.1111)  # 取2位有效数字,自动转换为科学计数法
output:1.1e+03

内置round

round(number[, ndigits])
参数:number - 这是一个数字表达式。ndigits - 表示从小数点到最后四舍五入的位数。默认值为0。
返回值。该方法返回x的小数点舍入为n位数后的值。

字符串输出

%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串

复制代码print('%s' % 'hello world') # 字符串输出 2 hello world

print('%s' % 'hello world')  # 字符串输出
hello world

print('%20s' % 'hello world')  # 右对齐,取20位,不够则补位
         hello world

print('%-20s' % 'hello world')  # 左对齐,取20位,不够则补位
hello world         

print('%.2s' % 'hello world')  # 取2位
he

print('%10.2s' % 'hello world')  # 右对齐,取2位
        he

print('%-10.2s' % 'hello world')  # 左对齐,取2位
he

其他

字符串格式代码

常用转义字符

2.format用法

位置匹配

(1)不带编号,即“{}”

(2)带数字编号,可调换顺序,即“{1}”、“{2}”

(3)带关键字,即“{a}”、“{tom}”

print('{} {}'.format('hello','world'))  # 不带字段
hello world

print('{0} {1}'.format('hello','world'))  # 带数字编号
hello world

print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
hello world hello

print('{1} {1} {0}'.format('hello','world'))
world world hello

print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字
world hello world

 

格式转换

'b' - 二进制。将数字以2为基数进行输出。

'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。

'd' - 十进制整数。将数字以10为基数进行输出。

'o' - 八进制。将数字以8为基数进行输出。

'x' - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。

'e' - 幂符号。用科学计数法打印数字。用'e'表示幂。

'g' - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。

'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。

'%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号。

print('{0:b}'.format(3))
11

print('{:c}'.format(20))


print('{:d}'.format(20))
20

print('{:o}'.format(20))
24

print('{:x}'.format(20))
14

print('{:e}'.format(20))
2.000000e+01

print('{:g}'.format(20.1))
20.1

print('{:f}'.format(20))
20.000000

print('{:n}'.format(20))
20

print('{:%}'.format(20))
2000.000000%

进阶用法

"int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)  
# 在前面加“#”,则带进制前缀
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

左中右对齐及位数补全

(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

(2)取位数“{:4s}”、"{:.2f}"等

print('{} and {}'.format('hello','world'))  # 默认左对齐
hello and world

print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐
hello      and      world

print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中间对齐
  hello    and   world   

print('{} is {:.2f}'.format(1.123,1.123))  # 取2位小数
1.123 is 1.12

print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小数,右对齐,取10位
1.123 is       1.12


'{:<30}'.format('left aligned')  # 左对齐
'left aligned                  '

'{:>30}'.format('right aligned')  # 右对齐
'                 right aligned'

'{:*>30}'.format('centered')
'**********************centered'

'{:^30}'.format('centered')  # 中间对齐
'           centered           '
'{:*^30}'.format('centered')  # 使用“*”填充
'***********centered***********'
'{:0=30}'.format(11)  # 还有“=”只能应用于数字,这种方法可用“>”代替
'000000000000000000000000000011'

正负符号显示 %+f, %-f, 和 % f的用法

'{:+f}; {:+f}'.format(3.14, -3.14)  # 总是显示符号
'+3.140000; -3.140000'

'{: f}; {: f}'.format(3.14, -3.14)  # 若是+数,则在前面留空格
' 3.140000; -3.140000'

'{:-f}; {:-f}'.format(3.14, -3.14)  # -数时显示-,与'{:f}; {:f}'一致
'3.140000; -3.140000'

占位符%s和%r----未拷贝过来

占位符嵌套     

逗号","分隔金钱,没以前进位       

时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值