Python的字符串格式化有两种方式: 百分号方式、format方式

百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。

----百分号

 tpl = "i am %s" % "alex"
#i am alex tpl = "i am %s age %d" % ("alex", 18)
#i am alex age 18 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
#i am alex age 18 tpl = "percent %.2f" % 99.97623
#percent 99.98 tpl = "i am %(pp).2f %%" % {"pp": 123.425556, }
#i am 123.43 %

----format

 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
#i am seven, age 18, alex
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
#i am seven, age 18, alex
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
#i am seven, age 18, really seven
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
#i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
#i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
#i am seven, age 18, really seven
tpl = "i am {0[0]}, age {1[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
#i am 1, age 22, really 3
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
#i am seven, age 18, money 88888.100000
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
#i am seven, age 18
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
#i am seven, age 18
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
#i am seven, age 18
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
#numbers: 1111,17,15,f,F, 1587.623000%
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
#numbers: 1111,17,15,f,F, 1587.623000%
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
#numbers: 1111,17,15,f,F, 1500.000000%
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
#numbers: 1111,17,15,f,F, 1500.000000%
import math
print('The value of PI is approximately {}.'.format(math.pi))
#The value of PI is approximately 3.14159265359.
print('The value of PI is approximately {!r}.'.format(math.pi))
#The value of PI is approximately 3.141592653589793.
print('The value of PI is approximately {0:.3f}.'.format(math.pi))
#The value of PI is approximately 3.142.
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
'''
Dcab ==> 7678
Jack ==> 4098
Sjoerd ==> 4127
'''
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
#Jack: 4098; Sjoerd: 4127; Dcab: 8637678

python 字符串格式化 ( 百分号 & format )的更多相关文章

  1. python字符串格式化方法 format函数的使用

      python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...

  2. python字符串格式化之format

    用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...

  3. (转)Python 字符串格式化 str.format 简介

    原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...

  4. 一文秒懂!Python字符串格式化之format方法详解

    format是字符串内嵌的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符串,一定程度上与%目的一致.但在某些方面更加的方便 1.基本用法 1.按照{}的顺序依次匹配括号中的值 s = &q ...

  5. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

  6. Python学习:12.Python字符串格式化

    字符串格式化 讲解Python这么久,也没有讲解Python的字符串的格式化,那我们今天就来了解一下python字符串格式化的强大之处. 首先我们先理解一下为什么要有字符串的格式化,就是为了方便字符串 ...

  7. python字符串格式化 %操作符 {}操作符---总结

    Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...

  8. 【转】Python字符串格式化

    Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sp ...

  9. 7. python 字符串格式化方法(2)

    7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...

随机推荐

  1. 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException

    场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...

  2. kubernetes环境下私有仓库搭建

    前期在客户那里搭建了基本运行环境,鉴于很多企业的环境都是内部网无法连接外部,因此搭建私有仓库是逃避不开的问题,按照网上的步骤搭建,虽然遇到一些问题,但还好都算容易解决了,下面大致把步骤记录一下便于下次 ...

  3. Actionscript 3 自定义 matedata

    metadata就是元数据 反应一个类本质的属性 可以通过describeType(obj)来得到反应该对象的xml 要自定义元数据,如[MyMatedata()] package {  public ...

  4. hbase集群安装和shell操作

    1.上传hbase安装包 2.解压 3.配置hbase集群,要修改3个文件(首先zk集群已经安装好了) 注意:要把hadoop的hdfs-site.xml和core-site.xml 放到hbase/ ...

  5. c# 用OpenXmL读取.xlsx格式的Excel文件 返回DataTable

    1.须要引用的dll :  DocumentFormat.OpenXml.dll  ---须要安装一下OpenXml再引用 WindowsBase  ---直接在项目里加入引用 2.方法: /// & ...

  6. Centos6.8 下 Node.js 的安装

    思路:采用编译好的文件进行安装 一 使用 wget 下载 到 Node.js 官网(https://nodejs.org/en/download/) 选择要下载的编译版本(Source Code) / ...

  7. Flume入门样例

    Flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original generation),属于 clo ...

  8. 监听器如何获取Spring配置文件

    我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...

  9. asp.net 表单数据提交,常见方式与错误总结

    在ASP中,我们通常把表单提交到另外一个页面(接受数据页面).但是在ASP.NET中,服务端表单通常都是提交到本页面的,如果我设置 form1.action="test.aspx" ...

  10. [UIDevice currentDevice].model

    iPhone Simulator iPad Simulator iPod touch iPad iPhone