转载自: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. [CSP-S模拟测试]:Set(随机化)

    题目描述 你手上有$N$个非负整数,你需要在这些数中找出一个非空子集,使得它的元素之和能被$N$整除.如果有多组合法方案,输出任意一组即可.注意:请使用高效的输入输出方式避免输入输出耗时过大. 输入格 ...

  2. js常用方法和检查是否有特殊字符串和倒序截取字符串

     js常用方法demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  3. three dots in git

    What are the differences between double-dot “..” and triple-dot “…” in Git commit ranges? Using Comm ...

  4. CATiledLayer显示超大图片的解决方案

    先对图片进行了裁剪 -> 很多小图片, 然后再根据显示 进行绘制 - (void)viewDidLoad { [super viewDidLoad]; [self cutImageAndSave ...

  5. linux下的命令是如何运行的

    linux下的命令分为内建命令.可执行文件.脚本文件 shell终端里键入一个命令,如ls.cd.bash,shell会先查询一个环境变量PATH,它存了各种可执行文件的路径,输入$PATH可以打印变 ...

  6. Mac版-Jdk安装与环境配置

    下载安装 oracle官网下载,地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 下载好后,点击安装包,一直 ...

  7. Cocos2d-x之Log输出机制

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在cocos2d-x中,我们使用log这个函数进行输出,log可以输出很多参数,它的使用方式就和使用c语言中的printf的使用方式差不多 ...

  8. [fw]Linux 的 time 指令

    Linux 的 time 指令   Linux 有個很有意思的 time 指令,可以用來查看另一個指令的執行時間,例如執行 time helloworld 會顯示 helloworld 這支程式的執行 ...

  9. NHibernet Unable to locate persister for the entity

    第一 xml文件必须为 *.hbm.xml 第二  设置xml文件为嵌入的资源,用鼠标点击右键 然后生成操作里 选择嵌入的资源即可解决. https://www.cnblogs.com/lyj/

  10. linux 命令 - ls(列出目录内容)

    ls - 列出目录内容 语法: ls (选项) (参数) 选项: -a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出): -A:显示除影藏文件“.”和“..”以外的所有文 ...