python 字符串模板_Python的字符串模板

python 字符串模板

Python is an interpreted, object-oriented, high-level programming language. It is easy to learn because its syntax emphasizes readability, which reduces the expense of program maintenance. Many programmers love working with Python because—without the compilation step—testing and debugging go quickly.​

Python是一种解释型,面向对象的高级编程语言 。 易于学习,因为它的语法强调可读性,从而减少了程序维护的费用。 许多程序员喜欢使用Python,因为无需进行编译步骤,即可快速进行测试和调试。

Python Web模板 ( Python Web Templating )

Templating, especially web templating, represents data in forms usually intended to be readable by a viewer. The simplest form of a templating engine substitutes values into the template to produce the output. 

模板,尤其是Web模板,以通常旨在让查看者阅读的形式表示数据。 模板引擎的最简单形式是将值替换到模板中以产生输出。

Aside from the string constants and the deprecated string functions, which moved to string methods, Python's string module also includes string templates. The template itself is a class that receives a string as its argument. The object instantiated from that class is called a template string object. Template strings were first introduced in Python 2.4. Where string formatting operators used the percentage sign for substitutions, the template object uses dollar signs.

除了字符串常量和不赞成使用的字符串函数(已移至字符串方法)外,Python的字符串模块还包括字符串模板。 模板本身是一个接收字符串作为其参数的类。 从该类实例化的对象称为模板字符串对象。 模板字符串最初是在Python 2.4中引入的。 在字符串格式运算符使用百分号代替的情况下,模板对象使用美元号。

  • $$ is an escape sequence; it is replaced with a single $.

    $$是转义序列; 它被替换为单个$

  • $<identifier> names a substitution placeholder matching a mapping key of <identifier>. By default, <identifier> must spell a Python identifier. The first non-identifier character after the $ character terminates this placeholder specification.

    $ <identifier>命名与<identifier>的映射键匹配的替换占位符。 默认情况下,<identifier>必须拼写Python标识符。 $字符后的第一个非标识符字符终止此占位符规范。

  • ${<identifier>} is equivalent to $<identifier>. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as ${noun}ification.

    $ {<identifier>}等同于$ <identifier>。 当有效的标识符字符位于占位符之后但不属于占位符时,例如$ {noun}化,则是必需的。

Outside of these uses of the dollar sign, any appearance of $ causes a ValueError to be raised. The methods available through template strings are as follows:

在美元符号的这些用途之外,任何出现的$都会引发ValueError。 通过模板字符串可用的方法如下:

  • Class string. Template(template): The constructor takes a single argument, which is the template string.

    字符串。 模板 ( template ):构造函数采用单个参数,即模板字符串。

  • Substitute(mapping, **keywords): Method that substitutes the string values (mapping) for the template string values. Mapping is a dictionary-like object, and its values may be accessed as a dictionary. If the keywords argument is used, it represents placeholders. Where both mapping and keywords are used, the latter takes precedence. If a placeholder is missing from mapping or keywords, a KeyError is thrown.

    替代 ( mapping,** keywords ):用字符串值( mapping)代替模板字符串值的方法。 映射是一个类似于字典的对象,其值可以作为字典来访问。 如果使用关键字参数,则表示占位符。 如果同时使用了映射关键字 ,则后者优先。 如果映射关键字中缺少占位符,则会抛出KeyError。

  • Safe_substitute(mapping, **keywords): Functions similarly to substitute(). However, if a placeholder is missing from mapping or keywords, the original placeholder is used by default, thus avoiding the KeyError. Also, any occurrence of "$" returns a dollar sign.

    安全 _ replace( 映射,**关键字 ):功能类似于replace()。 但是,如果映射关键字中缺少占位符,则默认情况下将使用原始占位符,从而避免了KeyError。 同样,任何出现的“ $”都会返回一个美元符号。

Template objects also have one publicly available attribute:

模板对象还具有一个公共可用的属性:

  • Template is the object passed to the constructor's template argument. While read-only access is not enforced, it is best not to change this attribute in your program.

    模板是传递给构造函数的模板参数的对象。 虽然不强制执行只读访问,但最好不要在程序中更改此属性。

The sample shell session below serves to illustrate template string objects.

下面的示例Shell会话用于说明模板字符串对象。

>>> from string import Template 
>>> s = Template('$when, $who $action $what.') 
>>> s.substitute(when='In the summer', who='John', action='drinks', what='iced tea') 'In the summer, John drinks iced tea.' 
>>> s.substitute(when='At night', who='Jean', action='eats', what='popcorn') 'At night, Jean eats popcorn.' 
>>> s.template '$when, $who $action $what.' 
>>> d = dict(when='in the summer') 
>>> Template('$who $action $what $when').safe_substitute(d) '$who $action $what in the summer' 

翻译自: https://www.thoughtco.com/pythons-string-templates-2813675

python 字符串模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值