一、string:文本常量和模板
 函数:capwords()
--------------------------------------------------
 import string
 s = 'The quick brown fox jumped over the lazy dog.'
 print(s)
 print(string.capwords(s))
--------------------------------------------------
The quick brown fox jumped over the lazy dog.
The Quick Brown Fox Jumped Over The Lazy Dog.
--------------------------------------------------
这个代码的结果等同于先调用split(),把结果列表中的单词首字母大写,然后调用join()来合并结果.
--------------------------------------------------
字符串模板,将作为内置拼接语法的替代做法.
   代码清单
--------------------------------------------------
import string
values = {'var': 'foo'}
t = string.Template("""
Variable         : $var
Escape           : $$
Variable in text : ${var}iable
""")
print('TEMPLATE:', t.substitute(values))
s = """
Variable         : $(var)s
Escape           : $$
Variable in text : ${var}iable
"""
print('INTERPOLATION:', s % values)
s = """
Variable         : {var}
Escape           : {{}}
Variable in text : ${var}iable
"""
print('FORMAT:', s.format(**values))
----------------------------------------------------
结果
----------------------------------------------------
TEMPLATE:
Variable         : foo
Escape           : $
Variable in text : fooiable
INTERPOLATION:
Variable         : $(var)s
Escape           : $$
Variable in text : ${var}iable
FORMAT:
Variable         : foo
Escape           : {}
Variable in text : $fooiable
----------------------------------------------------
safe_substitute()方法
----------------------------------------------------
代码清单
----------------------------------------------------
import string
values = {'var': 'foo'}
t = string.Template("$var is here but $missing is not provided")
try:
    print('substitute()    :', t.substitute(values))
except KeyError as err:
    print('ERROR:', str(err))
   
print('safe_substitute():', t.safe_substitute(values))
----------------------------------------------------
高级模板
----------------------------------------------------
delimiter和idpattern类属性。
----------------------------------------------------
代码清单
----------------------------------------------------
import string
class MyTemplate(string.Template):
    delimiter = '%'
    idpattern = '[a-z]+_[a-z]+'
   
template_text = '''
    Delimiter : %%
    Replaced  : %with_underscore
    Ignored   : %notunderscored
'''
d = {
    'with_underscore': 'replaced',
    'notunderscored': 'not replaced',
}
t = MyTemplate(template_text)
print('Modified ID pattern:')
print(t.safe_substitute(d))
-----------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 

《Python3-标准库》讲解的更多相关文章

  1. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  2. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  3. Spring MVC 入门示例讲解 - howtodoinjava

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  4. Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

    严重: StandardWrapper.Throwableorg.springframework.beans.factory.BeanCreationException: Error creating ...

  5. Spring入门(十三):Spring MVC常用注解讲解

    在使用Spring MVC开发Web应用程序时,控制器Controller的开发非常重要,虽然说视图(JSP或者是Thymeleaf)也很重要,因为它才是直接呈现给用户的,不过由于现在前端越来越重要, ...

  6. [Spring MVC] - Annotation验证

    使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotat ...

  7. Spring MVC — @RequestMapping原理讲解-1

    转载地址 :http://blog.csdn.net/j080624/article/details/56278461 为了降低文章篇幅,使得文章更目标化,简洁化,我们就不例举各种@RequestMa ...

  8. ASP.NET MVC 4 技术讲解

    ASP.NET MVC 相关的社群与讨论区 Routing 与 ASP.NET MVC 生命周期 Model相关技术 Controller相关技术 View数据呈现相关技术 Area区域相关技术 AS ...

  9. Spring MVC深入讲解

    一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...

  10. Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

    原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.b ...

随机推荐

  1. cocos2dx基础篇(25) 简单碰撞检测

    [3.x] 将数学类 CCPoint.CCRect 改为v3.x版本的 Vec2.Rect 就好了. [简单碰撞检测] 在一些游戏中经常会遇到碰撞检测的情况,如愤怒的小鸟飞出去后,是否与石头发生碰撞. ...

  2. python目录和引用关系

    这是我的项目目录 像这样引用没有直接画横线   但是运行时会报错:找不到 typeidea.typeidea.文件路径 图片拖出来看更清晰 后期补充: 解决方案: 如:右击:typeidea----- ...

  3. tensorflow增强学习应用于一个小游戏

    首先需要安装gym模块,提供游戏的. 1,所需模块 import tensorflow as tf import numpy as np import gym import random from c ...

  4. C++ 多文件编译简述:头文件、链接性、声明与定义

    目录 Commen Sense 头文件 链接性 static 与链接性控制 extern 与外部链接性 Reference Commen Sense C++ 在编译时对每个翻译单元(Translati ...

  5. HDU 1003 Max Sum (动态规划 最大区间和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. Android事件监听(一)——简介篇

    Button.ImageButton事件 setOnClickListener     点击时触发 ListView事件 setOnItemSelectedListener   鼠标滚动时触发 set ...

  7. [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化

    这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...

  8. eclipse project--->clean作用

    eclipse project-->clean  ,clean主要是class文件删除,并同时编译新的工程,生成新的class文件. 如果修改代码后,在运行时,还是旧代码,可能class文件还是 ...

  9. laravel修改用户模块的密码验证

    做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users 之外的用户表进行认证,那么就需要多做一点工作来完成这个功能. 现在假设我们只需要修改登录用户的表,表名和表结构 ...

  10. luogu P2093 [国家集训队]JZPFAR

    传送门 要维护平面上点的信息,所以可以用KD-tree来维护,然后维护一个大小为\(k\)的堆,每次从根开始遍历,遇到一个点就看能不能作为前\(k\)远的点,也就是看能不能把堆中最近的点给替换掉.如果 ...