7. python 字符串格式化方法(1)
7. python 字符串格式化方法(1)
承接上一章节,我们这一节来说说字符串格式化的另一种方法,就是调用format()
>>> template='{0},{1} and {2}'
>>> template.format ('a','b','c')
'a,b and c'
>>> template='{name1},{name2} and {name3}'
>>> template.format (name1='a',name2='b',name3='c')
'a,b and c'
>>> template='{name1},{0} and {name2}'
>>> template.format ('a',name1='b',name2='c')
'b,a and c'
>>>
这里根据上面的例子说明一下
1.替换的位置可以使用下标的来标记
2.替换的位置可以使用名称来替换
下面我们来说说,在方法里面添加属性
>>>import sys
>>> 'my {1[spam]} runs {0.platform}'.format(sys,{'spam':
'laptop'})
'my laptop runs win32'
>>>
>>> 'my {config[spam]} runs {sys.platform}'.format(sys=sys,config={'spam':'laptop'})
'my laptop runs win32'
>>>
上面两个例子里面,第一处读取了字符串,第二处读取sys里面的platform属性
下面再举一个例子,说明在表达式里面使用偏移量
>>> aList=list('abcde')
>>> aList
['a', 'b', 'c', 'd', 'e']
>>> 'first={0[0]} third={0[2]}'.format (aList)
'first=a third=c'
>>>
注意:在使用偏移量的时候只能够是正整数,不能够使用负数,不能够使用代表区间正整数
>>> aList=list('abcde')
>>> aList
['a', 'b', 'c', 'd', 'e']
>>> 'first={0[0]} third={0[-1]}'.format (aList)
Traceback (most recent call last):
File "", line 1, in
'first={0[0]} third={0[-1]}'.format (aList)
TypeError: list indices must be integers, not str
>>> 'first={0[0]} third={0[1:3]}'.format (aList)
Traceback (most recent call last):
File "", line 1, in
'first={0[0]} third={0[1:3]}'.format (aList)
TypeError: list indices must be integers, not str
>>>
7. python 字符串格式化方法(1)的更多相关文章
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
- python字符串格式化方法 format函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...
- python字符串格式化方法%s和format函数
1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...
- 第3.8节 Python百分号占位符的字符串格式化方法
一. 概念 格式化字符串就是将一些变量转换为字符串并按一定格式输出字符串,包括指定字符的位置.对齐方式.空位补充方式等.Python提供了多种字符串格式设置方法.本节先介绍一种简 ...
- 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别
<Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...
- Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?
Python中使用百分号占位符的字符串格式化方法中%s和%r表示需要显示的数据对应变量x会以str(x)还是repr(x)输出内容展示. 关于str和repr的关系请见: <Python中rep ...
- python的三种字符串格式化方法
1.最方便的 print 'hello %s and %s' % ('df', 'another df') 但是,有时候,我们有很多的参数要进行格式化,这个时候,一个一个一一对应就有点麻烦了,于是就有 ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- 6. python 字符串格式化表达式
6. python 字符串格式化表达式 字符串格式化允许在一个单个的步骤中对一个字符串执行多个特定类型的替换 特别是给用户提示的时候,格式化非常方便 实现方法: 1.格式化表达式,类似于c语言的pri ...
随机推荐
- Java关于数组操作函数
数组排序及元素查找 sort()方法对Java数组进行排序. binarySearch() 方法来查找数组中的元素,返回该元素所在的位置. import java.util.*; public cla ...
- mongo blancer
在 sharded cluster 体系结构中,Balancer 进程的作用是转移数据,当一个 shard 中的数据比其它 shard 的数据多并达到一定条件时,Balancer 进程触发. 为了减少 ...
- python开发学习-day16(Django框架初识)
s12-20160507-day16 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- (转)看懂UML类图
转自:http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- JDK源码分析(一)——ArrayList
目录 ArrayList分析 ArrayList继承结构 ArrayList字段属性 ArrayList构造函数 重要方法 ArrayList Iterator迭代器 总结 ArrayList分析 ...
- python流行的原因
12~14年是云计算最火的几年,大批创业公司和巨头挤破头地进军云计算领域,大家都在做IAAS,最著名的云计算开源平台OpenStack 就是基于Python 开发的,为此催生出不少Python 岗位 ...
- Win7/Win10下搭建Go语言开发环境
1 下载适合window版本的Go安装包,下载地址http://code.google.com/p/go/downloads/list 2 下载适合window本本的LiteIDE,下载后解压即可使用 ...
- 【十月のA Letter to 后辈菌】
文化课没什么好说的,那就不说了吧(^▽^) 以下送给下两级的学弟学妹们: 马上就要NOIP了,现在自己从戏中人变成了看戏人.不管高一还是高二,我感觉只要能走到这一步就很了不起了,不管结果如何,比赛过程 ...
- PHP-FPM高负载的解决办法
=================== 优化设置 ========================= When you running a highload website with PHP-FPM ...