【Python学习笔记】格式化输出和取整

一、格式化输出

1.格式化输出之——print

print'%d' % 20#十进制
20
print'%o' % 20#八进制
24
print'%x' % 20#十六进制
14
print('%e' % 1.123)  # 科学计数法(默认保留6位小数)
1.230000e+00
print("%.2g" % 1.35)  #两位有效数字,使用小数方式(默认保留6位小数)
1.4
print("%.1f" % 1.35)  #浮点数(默认保留6位小数)
1.3

2. 格式化输出之——format

print('{:b}'.format(2022))     # 二进制
print('{:o}'.format(2022))    # 八进制
print('{:d}'.format(2022))    # 十进制
print('{:x}'.format(2022))    # 十六进制
print('{:c}'.format(2022))    # 对应的Unicode字符串
print('{:e}'.format(2022))    # 科学计数法
print('{:g}'.format(2022.2))  # 小数
print('{:f}'.format(2022))    # 浮点数
print('{:n}'.format(2022))    # 当值为整数时和'd'相同,值为浮点数时和'g'相同。
print('{:-10.2%}'.format(1.5))   # 百分数

在这里插入图片描述

实战练习!!!
b = []
data = [[0,0.922584,0.537336,0.0384615,0.0720395 ]]
b = '({:.3g},{:.3g}),({:.3g},{:.3g})'.format(*data[0][1:])
print(b)
(0.923,0.537),(0.0385,0.072)
i = 1
print(f'这样也可以{i}')
这样也可以1
a = [1,2,3]
b = ['a',5,6]
c = zip(a,b)
for x,y in c:
	print("x是{},类型是{}".format(x,type(x)))
	print("y是{},类型是{}".format(y,type(y)))
x是1,类型是<class 'int'>
y是a,类型是<class 'str'>
x是2,类型是<class 'int'>
y是5,类型是<class 'int'>
x是3,类型是<class 'int'>
y是6,类型是<class 'int'>

二、取整

1. 向上取整

#需要导入math模块
import math
math.ceil(1.4)
2

2. 向下取整

#需要导入math模块
import math
math.floor(1.4)
1

推荐链接

三、对齐

1. ljust、center、rjust

# 左对齐,填充*
print('|','Hello'.ljust(10,'*'), '|')
# 居中对齐,填充*
print('|','Hello'.center(10,'*'), '|')
# 右对齐,填充*
print('|','Hello'.rjust(10,'*'), '|')

在这里插入图片描述

2. %(+/-)

# 左对齐
print(('\n' + '%-10s'*3) % ('Epoch', 'gpu_men', 'box'))
# 右对齐
print(('\n' + '%10s'*3) % ('Epoch', 'gpu_men', 'box'))

在这里插入图片描述

3. ^、<、>.

print('{:^10.2%}'.format(1.5))
print('{:<10.2%}'.format(1.5))
print('{:>10.2%}'.format(1.5))

在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修炼清爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值