python 格式化输出format的一些用法/pow/abs/round20200312

字符串与数字类型转换:

int float str

coord = (3, 5)
print('X: {0[0]};  Y: {0[1]}'.format(coord))  # X: 3;  Y: 5
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]};  Y: {0[b]}'.format(a))  # X: test_a;  Y: test_b

这个程序挺有意思可以研究研究。

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
# 通过位置匹配
print('{0}, {1}, {2}'.format('a', 'b', 'c')) # a, b, c
print('{}, {}, {}'.format('a', 'b', 'c'))  # 3.1+版本支持 a, b, c
print('{2}, {1}, {0}'.format('a', 'b', 'c')) # c, b, a
print('{2}, {1}, {0}'.format(*'abc'))  # 可打乱顺序 c, b, a
print('{0}{1}{0}'.format('abra', 'cad'))  # 可重复 abracadabra
print("{1},{0}".format("hello","world"))
#中间是.来衔接#{}{}用两个这个来传递格式化输出
#{1},{0}用标有序号01的代表format中的字符串的位置,从0开始。


幂 power引出pow

绝对值absolute value引出abs

round 引出四舍五入

round( x [, n]  )

  • x -- 数值表达式。
  • n -- 数值表达式,表示从小数点位数。
print(round(3.1415))##默认是保留整数

print(round(3.1415,1))##保留一位数

其他:英文单词的积累

Replacement 替代

positional  位置的

round 圆的

absolute绝对的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值