zoukankan      html  css  js  c++  java
  • string模块

    string 模块保留的一些用来处理 str 对象的常量和类

    string.capwords()  字符串的所有单词首字大写

    import string
    
    s = 'When the cat is away, the mice will play.'
    
    print(s.title())            # 字符串的所有单词首字母大写
    print(string.capwords(s))   # 字符串的所有单词首字母大写
    print(' '.join([i.capitalize() for i in s.split(' ')]))   # split()返回的列表每个单词的首字母大写再join
    # When The Cat Is Away, The Mice Will Play.
    # When The Cat Is Away, The Mice Will Play.
    # When The Cat Is Away, The Mice Will Play.

    模板

    定义模板

    使用 safe_substitute() 方法可以带来一个好处,那就是如果模板需要的值没有全部作为参数提供给模板的话可以避免发生异常。

    import string
    value = {'var':'foo'}
    
    t = string.Template('''
    Variable        :$var
    Escape          :$$
    Variable in text:${var}
    ''')
    print('TEAMPLATE:',t.substitute(value))
    
    '''
    TEAMPLATE: 
    Variable        :foo
    Escape          :$
    Variable in text:foo
    '''
    value = {'var':'foo'}
    s = """
    Variable        : %(var)s
    Escape          : %%
    Variable in text: %(var)siable
    """
    
    print('INTERPOLATION:', s % value)
    
    '''
    INTERPOLATION: 
    Variable        : foo
    Escape          : %
    Variable in text: fooiable
    '''
    values = {'var':'foo'}
    s = """
    Variable        : {var}
    Escape          : {{}}
    Variable in text: {var}iable
    """
    
    print('FORMAT:', s.format(**values))
    
    '''
    FORMAT: 
    Variable        : foo
    Escape          : {}
    Variable in text: fooiable
    '''
  • 相关阅读:
    IplImage, CvMat, Mat 的关系
    neon memory copy
    基于v4l2的webcam应用, 本地预监
    makefile写法实例
    Ubuntu 12.04 使用Eclipse搭建C/C++编译环境
    xapp1167与TRD14.4 关系
    v3学院带你一次性认清UART、RS-232、RS-422、RS-485的区别
    v3学院教你学习-task和function的异同
    寒假参加V3
    FPGA培训学习心得
  • 原文地址:https://www.cnblogs.com/st-st/p/9677547.html
Copyright © 2011-2022 走看看