Python - 字符串的替换(interpolation) 具体解释
字符串的插值(interpolation) 具体解释
本文地址: http://blog.csdn.net/caroline_wendy/article/details/27054263
字符串的替换(interpolation), 能够使用string.Template, 也能够使用标准字符串的拼接.
string.Template标示替换的字符, 使用"$"符号, 或 在字符串内, 使用"${}"; 调用时使用string.substitute(dict)函数.
标准字符串拼接, 使用"%()s"的符号, 调用时, 使用string%dict方法.
两者都能够进行字符的替换.
代码:
# -*- coding: utf-8 -*- '''
Created on 2014.5.26 @author: C.L.Wang Eclipse Pydev python 2.7.5
''' import string values = {'var' : 'foo'} tem = string.Template('''
Variable : $var
Escape : $$
Variable in text : ${var}iable
''') print 'TEMPLATE:', tem.substitute(values) str = '''
Variable : %(var)s
Escape : %%
Variable in text : %(var)siable
''' print 'INTERPOLATION:', str%values
输出:
TEMPLATE:
Variable : foo
Escape : $
Variable in text : fooiable INTERPOLATION:
Variable : foo
Escape : %
Variable in text : fooiable
Python - 字符串的替换(interpolation) 具体解释的更多相关文章
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...
- python 字符串中替换字符
今天本来打算写个程序,替换字符串中固定的一个字符:将<全部替换成回车'\n' 于是,我写成这样 s='sdjj<ddd<denj,>' for x in s: if x=='& ...
- Python 字符串多替换时性能基准测试
结论 先说结果, 直接替换是最好的. replace 一层层用, 方法笨了一点, 还可以. 时间消耗: tx2 < tx3 < tx1 < tx4 t2 < t3 < t ...
- python字符串replace失效问题
python字符串replace替换无效 背景 今天想把一个列表中符合条件的元素中 替换部分字符串, 发现怎么替换,改元素还是没有改变,本以为是内存引用的问题后来发现并不然. 经查阅解决 在Pytho ...
- Python - 字符串模板的安全替换(safe_substitute) 具体解释
字符串模板的安全替换(safe_substitute) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- python 字符串替换
字符串替换可以用内置的方法和正则表达式完成.1用字符串本身的replace方法: a = 'hello word'b = a.replace('word','python')print b 2用正则表 ...
- python字符串替换的2种有效方法
python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身 ...
- python字符串截取与替换的例子
python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...
随机推荐
- PHP获取表单方法
php接收HTML当表单提交的信息,数据将存储提交在全局阵列中,我们能够调用系统特定的自己主动全局变量数组来获取这些值.经常使用的自己主动全局变量例如以下所看到的: $_GET $_POST $_RE ...
- LeetCode219:Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- thinkphp3.2 代码生成并点击验证码
本人小菜鸟一仅仅.为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,server)等一系列的知识.小菜鸟创建了一个群.希望光临本博客的人能够进来交流.寻求 ...
- Spark大师之路:广播变量(Broadcast)源代码分析
概述 近期工作上忙死了--广播变量这一块事实上早就看过了,一直没有贴出来. 本文基于Spark 1.0源代码分析,主要探讨广播变量的初始化.创建.读取以及清除. 类关系 BroadcastManage ...
- VS公布 错 到文件失败 复制到
他自己和构建网站 ASP.MVC4 最近更改写功能 自此从未公布 已经报道 错 15 到文件失败 easyui\themes\gray\images\Thumbs.db 拷贝到 obj\Releas ...
- Java之旅(三)--- JSTL和EL表情
先给大家看一段JSP的代码.看看有什么感受? <% List<UsEL> usELList = pageModel.getList(); for (ItELator<Us ...
- java注解(转)
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy ...
- 设计模式16:迭代模式(Iterator)
迭代模式: 它提供了一种方法没有对象的顺序访问聚合对象的暴漏底层的细节. Provide a way to access the elements of an aggregate object seq ...
- Content://sms
package com.example.sms; import android.app.Activity; import android.app.Notification; import androi ...
- hexo 部署至Git遇到的坑
查找资料的时候发现了next这个博客主题,next!非常的漂亮,顺手查看了hexo的相关部署. Hexo官方介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲 ...