zoukankan      html  css  js  c++  java
  • 001.python-基础-Template的字符串格式化

    • Template是Python string模板中定义的一种字符串类型。
    • 用途:用于字符串替换操作。
    • 默认的定界符:$。
    # -*- coding: utf-8 -*-
    from string import Template
    
    # =============================================================================
    # 在python中Template可以将字符串的格式固定下来,重复利用。
    # Template属于string中的一个类,要使用他的话可以用以下方式调用:
    # from string import Template
    # 我们使用以下代码:
    # >>> s = Template('There  ${moneyType} is  ${money}')
    # >>> print s.substitute(moneyType = 'Dollar',money=12)
    # 
    # =============================================================================
    # define Template obj
    t = Template("CSDN ${name}")
    
    # string replace
    print (t.substitute({'name':'linduo'}))
    
    # 传入多个参数
    s= Template('my name is ${myname} ,age is ${myage}' )
    
    print (s.substitute(myname = 'lili' ,myage = '18'))
    
    # 如果只输入一个参数 s.safe_substitute
    print (s.safe_substitute(myname = 'wangwnag'))
  • 相关阅读:
    apache 修改文件上传大小限制
    linux 桥接模式下 固定ip 设置
    简易 红包生成
    Sublime Text 3 快捷键
    excle 文件的导入和导出
    php 下载文件
    右则css 小浮条
    html 自动跳转页面
    Media Queries详细
    mobile响应式页面meta信息相关记录
  • 原文地址:https://www.cnblogs.com/star521/p/13396287.html
Copyright © 2011-2022 走看看