zoukankan      html  css  js  c++  java
  • python 字符串方法

    string 包

    函数 描述
    string.ascii_letters 下文所述 ascii_lowercase 和 ascii_uppercase 常量的拼连。 该值不依赖于语言区域。
    string.ascii_lowercase 小写字母 'abcdefghijklmnopqrstuvwxyz'。 该值不依赖于语言区域,不会发生改变。
    string.ascii_uppercase 大写字母 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'。 该值不依赖于语言区域,不会发生改变。
    string.digits 字符串 '0123456789'。
    string.hexdigits 字符串 '0123456789abcdefABCDEF'。
    string.octdigits 字符串 '01234567'。
    string.punctuation 由在 C 区域设置中被视为标点符号的 ASCII 字符所组成的字符串: !"#$%&'()*+,-./:;<=>?@[]^_`{
    string.printable 由被视为可打印符号的 ASCII 字符组成的字符串。 这是 digits, ascii_letters, punctuation 和 whitespace 的总和。
    string.whitespace 由被视为空白符号的 ASCII 字符组成的字符串。 其中包括空格、制表、换行、回车、进纸和纵向制表符。

    内置的str处理函数

    str1 = "stringobject"
    str1.upper(); str1.lower(); str1.swapcase(); str1.capitalize(); str1.title()        # 全部大写,全部小写、大小写转换,首字母大写,每个单词的首字母都大写
    str1.ljust(width)                       # 获取固定长度,左对齐,右边不够用空格补齐
    str1.rjust(width)                       # 获取固定长度,右对齐,左边不够用空格补齐
    str1.center(width)                      # 获取固定长度,中间对齐,两边不够用空格补齐
    str1.zfill(width)                       # 获取固定长度,右对齐,左边不足用0补齐
    str1.find('t',start,end)                # 查找字符串,可以指定起始及结束位置搜索
    str1.rfind('t')                         # 从右边开始查找字符串
    str1.count('t')                         # 查找字符串出现的次数
    #上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
    str1.replace('old','new')               # 替换函数,替换old为new,参数中可以指定maxReplaceTimes,即替换指定次数的old为new
    str1.strip();                           # 默认删除空白符
    str1.strip('d');                        # 删除str1字符串中开头、结尾处,位于 d 删除序列的字符
    str1.lstrip();
    str1.lstrip('d');                       # 删除str1字符串中开头处,位于 d 删除序列的字符
    str1.rstrip();
    str1.rstrip('d')                        # 删除str1字符串中结尾处,位于 d 删除序列的字符
    str1.startswith('start')                # 是否以start开头
    str1.endswith('end')                    # 是否以end结尾
    str1.isalnum(); str1.isalpha(); str1.isdigit(); str1.islower(); str1.isupper()      # 判断字符串是否全为字符、数字、小写、大写
    

    字符串格式化

    %

    "this is %d %s bird" % (1, 'dead')                          # 一般的格式化表达式
    "%s---%s---%s" % (42, 3.14, [1, 2, 3])                      # 字符串输出:'42---3.14---[1, 2, 3]'
    "%d...%6d...%-6d...%06d" % (1234, 1234, 1234, 1234)         # 对齐方式及填充:"1234...  1234...1234  ...001234"
    x = 1.23456789
    "%e | %f | %g" % (x, x, x)                                  # 对齐方式:"1.234568e+00 | 1.234568 | 1.23457"
    "%06.2f" % x              									# 一共6位长度,不满足使用0填充,小数点后保留2位
    "% 6.2f" % x              									# 一共6位长度,不满足使用空格填充,小数点后保留2位
    "%04d" % 42                                                 # 一共4位长度,不满足使用0填充
    "% 4d" % 42                                                 # 一共4位长度,不满足使用空格填充
    "%(name1)d---%(name2)s" % {"name1":23, "name2":"value2"}    # 基于字典的格式化表达式
    "%(name)s is %(age)d" % vars()                              # vars()函数调用返回一个字典,包含了所有本函数调用时存在的变量
    

    format

    # 普通调用
    "{0}, {1} and {2}".format('spam', 'ham', 'eggs')            # 基于位置的调用
    "{motto} and {pork}".format(motto = 'spam', pork = 'ham')   # 基于Key的调用
    "{motto} and {0}".format('ham', motto = 'spam')             # 混合调用
    "{:.2f}".format(99.1234)                                    # 格式化浮点数 保留两位小数
    # 添加键 属性 偏移量 (import sys)
    "my {1[spam]} runs {0.platform}".format(sys, {'spam':'laptop'})                 # 基于位置的键和属性
    "{config[spam]} {sys.platform}".format(sys = sys, config = {'spam':'laptop'})   # 基于Key的键和属性
    "first = {0[0]}, second = {0[1]}".format(['A', 'B', 'C'])                       # 基于位置的偏移量
    # 具体格式化
    "{0:e}, {1:.3e}, {2:g}".format(3.14159, 3.14159, 3.14159)   # 输出'3.141590e+00, 3.142e+00, 3.14159'
    "{fieldname:format_spec}".format(......)
    # 说明:
    """
        fieldname是指定参数的一个数字或关键字, 后边可跟可选的".name"或"[index]"成分引用
        format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
        fill        ::=  <any character>              #填充字符
        align       ::=  "<" | ">" | "=" | "^"        #对齐方式
        sign        ::=  "+" | "-" | " "              #符号说明
        width       ::=  integer                      #字符串宽度
        precision   ::=  integer                      #浮点数精度
        type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
    """
    # 例子:
        '={0:10} = {1:10}'.format('spam', 123.456)    # 输出'=spam       =    123.456'
        '={0:>10}='.format('test')                    # 输出'=      test='
        '={0:<10}='.format('test')                    # 输出'=test      ='
        '={0:^10}='.format('test')                    # 输出'=   test   ='
        '{0:X}, {1:o}, {2:b}'.format(255, 255, 255)   # 输出'FF, 377, 11111111'
        'My name is {0:{1}}.'.format('Fred', 8)       # 输出'My name is Fred    .'  动态指定参数
    
  • 相关阅读:
    测 试 报 告模板
    浅谈如何设计自动化测试框架
    GET与POST类型接口
    测试用例设计总结
    Java OO知识总结
    Java基础知识总结
    MIT 6.824学习笔记3 Go语言并发解析
    Go语言_并发
    MIT 6.824学习笔记4 Lab1
    Leetcode Lect3 二分法总结
  • 原文地址:https://www.cnblogs.com/iFanLiwei/p/12782695.html
Copyright © 2011-2022 走看看