转载自:http://www.cnblogs.com/kaituorensheng/p/5709970.html

python自2.6后,新增了一种格式化字符串函数str.format(),威力十足,可以替换掉原来的%

:以下操作版本是python2.7

映射示例

语法

通过{} 和 :  替换 %

通过位置

>>> '{0} is {1}'.format('jihite', '4 years old')
'jihite is 4 years old'
>>> '{0} is {1} {0}'.format('jihite', '4 years old')
'jihite is 4 years old jihite'

通过format函数可以接受不限参数个数、不限顺序

通过关键字

>>> '{name}:{age}'.format(age=4,name='jihite')
'jihite:4'
>>> '{name}:{age}'.format(age=4,name='jihite',locate='Beijing')
'jihite:4'

format括号内用=给变量赋值

通过对象属性

>>> class Person:
... def __init__(self, name, age):
... self.name,self.age = name, age
... def __func__(self):
... return "This guy is {self.name}, is {self.age} old".format(self=self)
...
>>> s =Person('jihite', 4)
>>> s.__func__()
'This guy is jihite, is 4 old'

通过下标

>>> '{0[0]} is {0[1]} years old!'.format(['jihite', 4])
'jihite is 4 years old!'
>>> '{0} is {1} years old!'.format('jihite', 4)
'jihite is 4 years old!'

其实就是通过位置

格式限定符

通过{} : 符号

填充和对齐

^<>分别表示居中、左对齐、右对齐,后面带宽度

>>> '{:>10}'.format('jihite')
' jihite'
>>> '{:<10}'.format('jihite')
'jihite '
>>> '{:^10}'.format('jihite')
' jihite '

精度和类型f

精度常和f一起使用

>>> '{:.2f}'.format(3.1415)
'3.14'
>>> '{:.4f}'.format(3.1)
'3.1000'

进制转化

>>> '{:b}'.format(10)
'1010'
>>> '{:o}'.format(10)
'12'
>>> '{:d}'.format(10)
'10'
>>> '{:x}'.format(10)
'a'

其中b o d x分别表示二、八、十、十六进制

千位分隔符

>>> '{:,}'.format(1000000)
'1,000,000'

>>> '{:,}'.format(100000.23433)
  '100,000.23433'

>>> '{:,}'.format('abcedef')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Cannot specify ',' with 's'. 尤其是其中的精度与类型,用起来很方便

python format函数的使用的更多相关文章

  1. python format()函数的用法

    Python format() 函数的用法 复制自博主 chunlaipiupiupiu 的博客,如有侵权,请联系删除 python中format函数用于字符串的格式化 通过关键字 1 print(' ...

  2. python format函数/print 函数详细讲解(4)

    在python开发过程中,print函数和format函数使用场景特别多,下面分别详细讲解两个函数的用法. 一.print函数 print翻译为中文指打印,在python中能直接输出到控制台,我们可以 ...

  3. python format() 函数

    转载 https://www.cnblogs.com/wushuaishuai/p/7687728.html 正文 Python2.6 开始,新增了一种格式化字符串的函数 format() ,它增强了 ...

  4. python - format函数 /class内置format方法

    format函数 # format函数 # 用于字符串格式化 # 基本用法: # 方式一:(位置方式) x = "{0}{1}{2}".format(1,2,3) print('1 ...

  5. 第三章:Python基础の函数和文件操作实战

    本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...

  6. python range函数(42)

    在python中使用最多的除了print函数 就是 for循环 了,那么这里就不得不介绍一下python内置函数range函数! 一.range函数简介 python range函数可创建一个整数列表 ...

  7. 飘逸的python - 增强的格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  8. Python中用format函数格式化字符串的用法

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下   自python2. ...

  9. 【python】format函数格式化字符串的用法

    来源:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式 ...

随机推荐

  1. 几乎相同的 deal.jsp 代码(index.jsp不变),在IDEA相同项目运行,结果却不一样,实在想不出来

    目录 主要问题 主要项目 index.jsp: deal.jsp(正确可运行): deal.jsp(错误不可运行): 错误的代码运行图片: 可运行的代码运行图片 主要问题 几乎相同的 deal.jsp ...

  2. (转)Windows下zookeeper安装及配置

    转:https://blog.csdn.net/qq_36332827/article/details/79700239 zookeeper有单机.伪集群.集群三种部署方式,可根据自己对可靠性的需求选 ...

  3. LCA 总结

    代码: //RMQ求LCA struct node { int v, w; }; class LCA { private: vector<int>dep, pos, olx, dis; v ...

  4. upc组队赛16 WTMGB【模拟】

    WTMGB 题目链接 题目描述 YellowStar is very happy that the FZU Code Carnival is about to begin except that he ...

  5. SSD如何设置预留空间OP(Over-Provision)

    Over-Provision操作指南 SSD OP全称是(Over-Provision), 中文名预留空间, 指用户不可操作的容量,大小为SSD实际容量减去用户可用容量.简单来说over-provis ...

  6. “希希敬敬对”队软件工程第九次作业-beta冲刺第六次随笔

    “希希敬敬对”队软件工程第九次作业-beta冲刺第六次随笔 队名:  “希希敬敬对” 龙江腾(队长) 201810775001 杨希                   201810812008 何敬 ...

  7. C++异常处理的深入理解

    1,问题: 1,如果在 main 函数中抛出异常会发生什么? 1,不处理,则崩溃: 2,如果异常不处理,最后会传到哪里? 3,下面的代码输出什么? 4,异常的最终处理编程实验: #include &l ...

  8. Libgdx中TextButton的一些思考

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/caihongshijie6/article/details/37566183         由于有 ...

  9. 45.Sort List(链表排序)

    Level:   Medium 题目描述: Sort a linked list in O(n log n) time using constant space complexity. Example ...

  10. Python之基本的日期与时间转换 datetime、 dateutil模块

    简单举例datetime模块 from datetime import timedelta,datetime a = timedelta(days=2, hours=6) b = timedelta( ...