zoukankan      html  css  js  c++  java
  • 011 字符串的内置方法 三

    encode(encoding='utf-8', errors='strict')

    • 释义
      • encoding: 指定编码格式,默认为 utf-8
      • errors
        • 设置不同错误的处理方案,默认为 strict,意为编码错误引起一个 UnicodeError
        • 其他的值:ignore, replace, xmlcharrefreplace, backslashreplace
        • 还可以通过 codecs.register_error() 注册值
    >>> s = "YorkFish不是鱼"
    >>> utf8 = s.encode()
    >>> utf8
    b'YorkFishxe4xb8x8dxe6x98xafxe9xb1xbc'
    >>> utf8.decode()
    'YorkFish不是鱼'
    >>> 
    >>> gbk = s.encode("gbk")
    >>> gbk
    b'YorkFishxb2xbbxcaxc7xd3xe3'
    >>> gbk.decode("gbk")
    'YorkFish不是鱼'
    >>> 
    

    expandtabs([tabsize=8])

    • 释义:
      • 将字符串中的 转换为空格
      • 如果不指定参数,默认 1 个制表符转 8 个空格
    >>> s = "a tab: "
    >>> s
    'a tab:	'
    >>> s.expandtabs()
    'a tab:  '
    >>> s.expandtabs(16)
    'a tab:          '
    >>> 
    

    顺便补上 print()

    • print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)
    • 默认情况下,将值打印到流或 sys.stdout
    • 关键字参数
      • file: 类似于文件的对象(流);默认为当前的 sys.stdout
      • sep: 在值之间插入的字符串,默认为空格
      • end: 在最后一个值后追加的字符串,默认为换行符
      • flush: whether to forcibly flush the stream 是否强制刷新数据流
    first = "York"
    last = "Fish"
    
    print(first + last)
    print(first, last, sep='')
    print(first, last)
    print()
    print("YorkFish", end='
    
    ')
    print(first, end='-')
    print(last)
    

    >>>

    YorkFish
    YorkFish
    York Fish
    
    YorkFish
    
    York-Fish
    

    format_map()

    • 释义:在给定的字典中找对应的值

    例1 官网的例子

    class Default(dict):
         def __missing__(self, key):
             return key
    
    
    print('{name} was born in {country}'.format_map(Default(name='Guido')))
    

    >>>

    Guido was born in country
    

    例2

    >>> d = {"name": "YorkFish", "language": "Python"}
    >>> "{name} is learning {language}.".format_map(d)
    'YorkFish is learning Python.'
    >>> 
    

    replace(old, new[, count])

    • 释义
      • new 替换 old,有多少换多少
      • 如果指定 count,则替换不超过 count
    >>> s = "All things in their being are good for something."
    >>> s.replace('i', 'I')
    'All thIngs In theIr beIng are good for somethIng.'
    >>> s.replace('i', 'I', 3)
    'All thIngs In theIr being are good for something.'
    >>> 
    

    split(sep=None, maxsplit=-1)

    • 释义
      • sep 分割字符串
      • 默认以空格为分割符,有个陷阱,见例2
      • 如果设置了 maxsplit,则仅分割 maxsplit

    例1

    >>> "Y.o.r.k.F.i.s.h".split('.')
    ['Y', 'o', 'r', 'k', 'F', 'i', 's', 'h']
    >>> "Y.o.r.k.F.i.s.h.".split('.')
    ['Y', 'o', 'r', 'k', 'F', 'i', 's', 'h', '']
    >>> "Y.o.r.k.F.i.s.h".split('.', maxsplit=4)
    ['Y', 'o', 'r', 'k', 'F.i.s.h']
    >>> 
    

    例2

    >>> " yorkfisih".split()
    ['yorkfisih']
    >>> 
    >>> " yorkfisih".split(' ')
    ['', 'yorkfisih']
    >>> 
    >>> ''.split(' ')  # n 个空格会被分为 n+1 个部分
    ['']
    >>> ' '.split(' ')
    ['', '']
    >>> '  '.split(' ')
    ['', '', '']
    >>> 
    
    • rsplit(sep=None, maxsplit=-1) 用法类似,从右边开始分割

    splitlines([keepends])

    • 释义
      • 按照 分隔,返回一个包含各行作为元素的列表
      • keependsTrue,则保留换行符

    例1

    >>> s = "Cease to struggle 
    and you cease to live.
    (Thomas Carlyle)"
    >>> s.splitlines()
    ['Cease to struggle ', 'and you cease to live.', '(Thomas Carlyle)']
    >>> s.splitlines(True)
    ['Cease to struggle 
    ', 'and you cease to live.
    ', '(Thomas Carlyle)']
    >>> 
    

    例2 splitlines() 与 split()

    >>> ''.splitlines()
    []
    >>> ''.split('
    ')
    ['']
    >>> "love at first sight.
    ".splitlines()
    ['love at first sight.']
    >>> "love at first sight.
    ".split('
    ')
    ['love at first sight.', '']
    

    startswith(prefix[, start[, end]])

    • 释义
      • 检查字符串是否以指定前缀 prefix 开头
      • 返回 True or False
    >>> s = "The best is yet to come."
    >>> s.startswith("The")
    True
    >>> s.startswith("is", 9)
    True
    >>> s.startswith("best", 4, 8)  # [4, 8)
    True
    >>> s.startswith("best", 4, 7)
    False
    >>> 
    
    • endswith(sub[, start[, end]]) 见第一弹

    swapcase()

    • 释义:翻转字符串中的大小写
    >>> "YorkFish".swapcase()
    'yORKfISH'
    >>> 
    

    title()

    • 释义
      • 返回“标题化”的字符串
      • Python 只是单纯地使每个词的首字母大写,非首字母小写
      • 对标题而言,比如介词要保持原样,这个 title() 没有实现
    >>> "the great gatsby".title()
    'The Great Gatsby'
    >>> 
    

    zfill(width)

    • 释义
      • 必须填 width,否则会抛异常
      • 返回长度为 width 的字符串
      • 原字符串右对齐
      • width 大于字符串长度,前边用 0 填充
    >>> name = "YorkFish"
    >>> name.zfill(5)
    'YorkFish'
    >>> name.zfill(10)
    '00YorkFish'
    
  • 相关阅读:
    js积累点
    org.hibernate.NonUniqueObjectException
    SSH框架下的表单重复提交
    ajax传输中文参数乱码,本地使用tomcat不乱码,liunx+weblogic乱码
    启动weblogic报错:string value '2.4' is not a valid enumeration value for web-app-versionType in namespace http://java.sun.com/xml/ns/javaee
    weblogic启动项目,设置内容、设置的数据源链接不生效
    myeclipse编译弹框:The builder launch configuration could not be found
    tomcat启动项目报错:The specified JRE installation does not exist
    Mac上的jdk
    前端
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10268679.html
Copyright © 2011-2022 走看看