截取自官方文档

体会一下各种format方式的强大!

Accessing arguments by position:

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')

'a, b, c'

>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only

'a, b, c'

>>> '{2}, {1}, {0}'.format('a', 'b', 'c')

'c, b, a'

>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence

'c, b, a'

>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated

'abracadabra'

Accessing arguments by name:

>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')'Coordinates: 37.24N, -115.81W'

>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}

>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)

'Coordinates: 37.24N, -115.81W'

Accessing arguments’ attributes:

>>> c = 3-5j

>>> ('The complex number {0} is formed from the real part {0.real} '

... 'and the imaginary part {0.imag}.').format(c)

'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'

>>> class Point:

... def __init__(self, x, y):

... self.x, self.y = x, y

... def __str__(self):

... return 'Point({self.x}, {self.y})'.format(self=self)

...

>>> str(Point(4, 2))

'Point(4, 2)'

Accessing arguments’ items:

>>> coord = (3, 5)

>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)

'X: 3; Y: 5'

Replacing %s and %r:

>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')

"repr() shows quotes: 'test1'; str() doesn't: test2"

Aligning the text and specifying a width:

>>> '{:<30}'.format('left aligned')

'left aligned '

>>> '{:>30}'.format('right aligned')

' right aligned'

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

' centered '

>>> '{:*^30}'.format('centered') # use '*' as a fill char

'***********centered***********'

Replacing %+f, %-f, and % f and specifying a sign:

>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always

'+3.140000; -3.140000'

>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers

' 3.140000; -3.140000'

>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'

'3.140000; -3.140000'

Replacing %x and %o and converting the value to different bases:

>>> # format also supports binary numbers

>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)

'int: 42; hex: 2a; oct: 52; bin: 101010'

>>> # with 0x, 0o, or 0b as prefix:

>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)

'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'

Using the comma as a thousands separator:

>>> '{:,}'.format(1234567890)

'1,234,567,890'

Expressing a percentage:

>>> points = 19

>>> total = 22

>>> 'Correct answers: {:.2%}'.format(points/total)

'Correct answers: 86.36%'

Using type-specific formatting:

>>> import datetime

>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)

>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)

'2010-07-04 12:15:58'

Nesting arguments and more complex examples:

>>> for align, text in zip('', ['left', 'center', 'right']):

... '{0:{fill}{align}16}'.format(text, fill=align, align=align)

...'left<<<<<<<<<<<

'^^^^^center^^^^^'

'>>>>>>>>>>>right'

>>>

>>> octets = [192, 168, 0, 1]

>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)

'C0A80001'

>>> int(_, 16)

3232235521

>>>

>>> width = 5

>>> for num in range(5,12):

... for base in 'dXob':

... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')

... print()

...

5 5 5 101

6 6 6 110

7 7 7 111

8 8 10 1000

9 9 11 1001

10 A 12 1010

11 B 13 1011

python string.format()_python string format相关推荐

  1. python函数格式化_Python通过format函数格式化显示值

    Python通过format函数格式化显示值,小数,小数点,转换成,科学,参数 Python通过format函数格式化显示值 易采站长站,站长之家为您整理了Python通过format函数格式化显示值 ...

  2. python中格式化_python的format格式化

    使用方法:  '{}bbccc'.format(aa) = aabbcc, 用来代替python2中的%,即替换. 1.通过位置来指定替换 In [2]: '{0},{1}'.format('a', ...

  3. python string.format()_Python string.format()百分比,不取整

    In the example below I would like to format to 1 decimal place but python seems to like rounding up ...

  4. python表格对齐_python str.format 中文对齐的细节问题,

    python str.format 中文对齐的细节问题, 写了一个练手的爬虫...在输出的时候出现了让人很不愉♂悦的问题 像这样: 令人十分难受啊! #------------------------ ...

  5. python字符串库函数_Python标准库概览(1):string

    Python的 string 标准库保留了一些有用的函数和用于处理文本对象的类,现在我们来一起看一下Python的string标准库还有哪些我们不知道的有趣用法? 01.capwords()函数:将字 ...

  6. python中uppercase是什么意思_Python string.ascii_uppercase方法代码示例

    本文整理汇总了Python中string.ascii_uppercase方法的典型用法代码示例.如果您正苦于以下问题:Python string.ascii_uppercase方法的具体用法?Pyth ...

  7. python中uppercase是什么意思_Python string.uppercase方法代码示例

    本文整理汇总了Python中string.uppercase方法的典型用法代码示例.如果您正苦于以下问题:Python string.uppercase方法的具体用法?Python string.up ...

  8. warning: format not a string literal and no format arguments

    在学习Object-c的时候,按着书上的代码出现"warning: format not a string literal and no format arguments": 虽然 ...

  9. Error format not a string literal and no format arguments解决方案

    From: http://www.cnblogs.com/hhuang2012/p/3336911.html 场景: cocos2dx 跨平台开发, 移植Android版本时, 当进行到build_n ...

最新文章

  1. 用Python抓取某东购买记录并统计MM的bra大小
  2. 聊一聊:拿到年终奖后马上辞职,厚道吗?
  3. java 锁_Java 锁之我见
  4. Spring MVC-06循序渐进之Converter和Formatter
  5. Cifar10与ResNet18实战、lenet5、resnet(学习笔记)
  6. 近期工作中使用到的插件总结
  7. 软考信息安全工程师培训精品课-更新中
  8. 虚拟资源拳王公社:闲鱼虚拟资源玩法案例拆解,教你玩转虚拟资源,货源+方法
  9. 关机状态下启动微型计算机叫什么,教你电脑关机后自动重启是什么原因
  10. 【编译原理笔记08】语法制导翻译:语法制导定义,SSD的求值顺序,S属性定义与L属性定义
  11. CS231n李飞飞计算机视觉 卷积神经网络详解下
  12. matlab中sl设置频率为95khz,BOOST电路设计及matlab仿真
  13. 古筝d调变降e调怎么办_为什么古筝总要调音、还总调不好?
  14. XPIR : Private Information Retrieval for Everyone论文阅读笔记
  15. “七段数码管绘制”实例详解
  16. 修改Linux时间一般涉及到3个命令: date, clock, hwclock
  17. eclipse怎么搜索关键字? eclipse查找关键字的技巧
  18. c++之 std::tie
  19. 【UE4 第一人称射击游戏】08-使用“AK47”发射子弹
  20. 微信支付平台技术文档的一个小坑

热门文章

  1. python idls_Python argparse模块实现模拟 linux 的ls命令
  2. 琉璃男主成毅手机壁纸,你要么?
  3. 设计导航网站|解决寻找合适的字体麻烦
  4. 设计配色专辑,很值得设计师拥有
  5. win7变成xp风格了怎么改回_让电脑提速的几种方法(老电脑太卡怎么提速)
  6. 触发器_触发器第三弹
  7. oracle 计算复杂 数据跑不出来 如何分批次_如何配置PG的数据库缓冲
  8. pcm转换在线工具_有木有好用的CAD格式转换工具可以推荐?在线等,挺急的
  9. 爬虫项目之爬取页面并按界面样式导入excel表格
  10. 在使用代理的服务器上“curl: (6) Could not resolve host:“问题的解决方案