码迷,mamicode.com
首页 > 编程语言 > 详细

【Rollo的Python之路】Python:格式化输出:%与format

时间:2019-04-26 22:52:00      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:用法   ash   span   数字   十进制   %x   格式   顺序   整数   

%用法:

1.0 整数的输出

%o —— oct 八进制
%d —— dec 十进制
%x —— hex 十六进制

>>> print(‘%o‘ % 20)
24
>>> print(‘%d‘ % 20)
20
>>> print(‘%x‘ % 20)
14

2.0 浮点数输出 %f

3.0 字符串输出 %d

>>> print(‘%s‘ % ‘hello world‘)  # 字符串输出
hello world
print("%(u)s" % {‘u‘:"sdads"}) #可以用字典与表达

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

 

【Rollo的Python之路】Python:格式化输出:%与format

标签:用法   ash   span   数字   十进制   %x   格式   顺序   整数   

原文地址:https://www.cnblogs.com/rollost/p/10776848.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!