python 字符串格式化 ( 百分号 & format )
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 )的更多相关文章
- python字符串格式化方法 format函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...
- python字符串格式化之format
用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...
- (转)Python 字符串格式化 str.format 简介
原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...
- 一文秒懂!Python字符串格式化之format方法详解
format是字符串内嵌的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符串,一定程度上与%目的一致.但在某些方面更加的方便 1.基本用法 1.按照{}的顺序依次匹配括号中的值 s = &q ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- Python学习:12.Python字符串格式化
字符串格式化 讲解Python这么久,也没有讲解Python的字符串的格式化,那我们今天就来了解一下python字符串格式化的强大之处. 首先我们先理解一下为什么要有字符串的格式化,就是为了方便字符串 ...
- python字符串格式化 %操作符 {}操作符---总结
Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...
- 【转】Python字符串格式化
Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sp ...
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
随机推荐
- 折腾kubernetes各种问题汇总
折腾fluend-elasticsearch日志,折腾出一大堆问题,解决这些问题过程中,感觉又了解了不少. 1.如何删除不一致状态下的rc,deployment,service. 在某些情况下,经常发 ...
- (转)spring boot实战(第三篇)事件监听源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48194777 监听源码分析 首先是我们自定义的main方法: package com.lkl. ...
- SRS Audio Sandbox没有声音怎么办
首先介绍下这款软件呵: SRS Audio Sandbox是一款个人电脑终极音频增强软件.该软件可以提供令人叹为观止的环绕音效,重低音效果并且非常清晰,甚至可以用於桌面扬声器.可以作用於个人电脑上的所 ...
- Qt Creator项目中使用qss
近期学习qt .使用的编译器是qt creator ,学习过程中遇到的题就是 怎样将程序中将要用到的.qss 文件静态编译到.exe程序中,而不是在程序执行时动态加载.动态加载的最大问题在于一旦.qs ...
- Tomcat 服务器只能存有一个正在运行的项目
即使新建了一个new project (在同一个工作空间),启动Tomcat 还是会出现先前(工程名)一样的问题/异常. [原因]: 在底下Server 那里——Tomcat 7.X 底下会有很多工程 ...
- 神技do{}while(false)
神技do{}while(false) do{}while(false)或者说do{}while(0),本人在linux源码中学得,起初看起来比较奇怪,但在处理连续流程中特别有用,例如ABC三个流程,A ...
- MVC之ActionFilterAttribute自定义属性
ActionFilterAttribute里有OnActionExecuting方法,跟Controller一样, 同是抽象实现了IActionFilter接口. // 登录认证特性 public c ...
- vue 父子组件属性传递
父子组件属性传递 注意:0.谁被引用,谁就算子组件 1.属性命名最好完全小写,否则需要如下格式转换:myAttr == my-attr 2.引入的vue组件后必须通过 components 注册才能 ...
- unity, 自定义类中使用print
在unity脚本中自定义c#类,而且不继承MonoBehaviour的话,若还想在其中使用print函数,可以用MonoBehaviour.print(...).
- Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结
Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1.1. C# 1.0-纯粹的面向对象 1.2. C# 2.0-泛型编程新概念 1.3. ...