博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 字符串模板_Python字符串模板
阅读量:2529 次
发布时间:2019-05-11

本文共 3513 字,大约阅读时间需要 11 分钟。

python 字符串模板

Python String Template class is used to create a simple template string, where fields can be replaced later on to create a string object. We can use function to create a string too. However, format() function provides a lot of options and in some situations, we want the simple replacement features, for example, Internationalization (i18n). In these cases, template string is useful and easy to use.

Python String Template类用于创建简单的模板字符串,以后可以在其中替换字段以创建字符串对象。 我们也可以使用函数创建一个字符串。 但是,format()函数提供了很多选项,并且在某些情况下,我们需要简单的替换功能,例如,国际化(i18n)。 在这些情况下,模板字符串非常有用且易于使用。

Python字符串模板 (Python String Template)

Template is created by passing template string to its constructor. Python template strings support $-based substitutions.

通过将模板字符串传递到其构造函数来创建模板。 Python模板字符串支持基于$的替换。

Template class has two functions to create a string from the template.

模板类具有两个从模板创建字符串的功能。

  1. substitute(mapping, **kwds): performs substitution from the like key based mapping object or from the keyword arguments. If both mapping and keyword arguments have same key, then TypeError is thrown. The error message will look like TypeError: substitute() got multiple values for keyword argument 'aaa'. If the key is not provided, then KeyError will be raised.

    replace(mapping,** kwds) :从像基于键的映射对象之类的或从关键字参数中执行替换。 如果映射参数和关键字参数都具有相同的键,则将引发TypeError。 错误消息将看起来像TypeError: substitute() got multiple values for keyword argument 'aaa' 。 如果未提供密钥,则将引发KeyError
  2. safe_substitute(mapping, **kwds): behaves just like substitue() method, except when key is not found then it doesn’t raise KeyError and placeholder is returned in the result string.

    safe_substitute(mapping,** kwds) :行为类似于substitue()方法,除了找不到键时,它不会引发KeyError,并且在结果字符串中返回占位符。

Python模板字符串示例 (Python Template String Example)

Let’s look at simple example of template string in python.

让我们看一下python中模板字符串的简单示例。

from string import Templatet = Template('$name is the $job of $company')s = t.substitute(name='Tim Cook', job='CEO', company='Apple Inc.')print(s)# dictionary as substitute argumentd = {"name": "Tim Cook", "job": "CEO", "company": "Apple Inc."}s = t.substitute(**d)print(s)

Output:

输出:

Tim Cook is the CEO of Apple Inc.Tim Cook is the CEO of Apple Inc.

safe_substitute()示例 (safe_substitute() example)

from string import Templatet = Template('$name is the $job of $company')s = t.safe_substitute(name='Tim Cook', job='CEO')print(s)

Output: Tim Cook is the CEO of $company

输出: Tim Cook is the CEO of $company

打印模板字符串 (Printing template string)

Template object has “template” attribute that returns the template string.

模板对象具有返回模板字符串的“ template”属性。

t = Template('$name is the $job of $company')print('Template String =', t.template)

Output: Template String = $name is the $job of $company

输出: Template String = $name is the $job of $company

转义$符号 (Escaping $ sign)

We can use $$ to escape $ sign and treat it as part of normal string.

我们可以使用$$来转义$符号并将其视为普通字符串的一部分。

t = Template('$$ is called $name')s = t.substitute(name='Dollar')print(s)

Output: $ is called Dollar

输出: $ is called Dollar

$ {identifier}示例 (${identifier} example)

${identifier} is same as $identifier. It’s required when valid identifier characters follow the placeholder but are not part of the placeholder. Let’s understand this with a simple example.

$ {identifier}与$ identifier相同。 当有效的标识符字符位于占位符之后但不是占位符的一部分时,这是必需的。 让我们用一个简单的例子来理解这一点。

t = Template('$noun adjective is ${noun}ing')s = t.substitute(noun='Test')print(s)

Output: Test adjective is Testing

输出: Test adjective is Testing

. 检出完整的脚本和更多Python字符串示例。

翻译自:

python 字符串模板

转载地址:http://iamzd.baihongyu.com/

你可能感兴趣的文章
你必须了解的java内存管理机制(四)-垃圾回收
查看>>
BZOJ4719 NOIP2016天天爱跑步(线段树合并)
查看>>
前端打包文件在 nginx 上 403 的解决办法
查看>>
JFreeChart(一)
查看>>
python--12、pymysql模块
查看>>
洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)
查看>>
PHP 超级全局变量
查看>>
1*1卷积核的理解和作用
查看>>
win32_c语言绘制曼德博集合(分形绘制)
查看>>
wc之上传图片
查看>>
程序员的核心技能是短期记忆力
查看>>
四.rocketMQ原理
查看>>
【原创】自己动手写的一个查看函数API地址的小工具
查看>>
sd卡的操作
查看>>
mui-当使用addeleventlisener()方法绑定事件时选择器无法绑定事件
查看>>
javascript 中的var : 隐含变量 全局变量 变量提升
查看>>
阿里巴巴Json工具-Fastjson讲解
查看>>
POJ 2376 (区间问题,贪心)
查看>>
Xtreme8.0 - Kabloom 动态规划
查看>>
Cloudera Manager5安装总结遇到问题及解决办法 CDH 5.8 on CentOS 7
查看>>