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'))
  • 相关阅读:
    NOIP simulation
    NOIP2013
    BZOJ 4176 Lucas的数论
    BZOJ 3512 DZY Loves Math IV
    BZOJ 3994 Sum
    BZOJ 4174 tty的求助
    BZOJ 3561 DZY Loves Math VI
    BZOJ 2508 简单题/BZOJ 3775 点和直线
    FTR #1 百步穿杨
    BZOJ 4407 于神之怒加强版
  • 原文地址:https://www.cnblogs.com/star521/p/13396287.html
Copyright © 2011-2022 走看看