zoukankan      html  css  js  c++  java
  • python数据类型--字符串

    字符串方法 返回值类型 方法详情
    .capitalize() str 首字母大写
    .casefold() str 所有字符小写(不仅仅是A-Z)
    .center(width[, fillchar]) str 居中并填充fillchar(默认为空格)至长度为width
    .count(sub[,start[,end]]) int 返回sub出现在范围内的次数,范围可选
    .encode(encoding='utf-8', errors='strict') bytes

    以encoding指定的编码对字符串编码. errors: strict, 代表遇到非法字符时抛出异常;

    ignore, 则会忽略非法字符; replace, 则会用?取代非法字符; xmlcharrefreplace,则使用XML的字符引用.  

    .endswith(suffix[,start[,end]])  bool 判断字符串在范围内是否以suffix结束,是True,否False. 可传入元组,只要有一个是True就返回True
    .expandtabs([tabsize=8])  str

    把tab符号( )转换为对应长度的空格, 或 会使空格数从零开始.

    如不指定参数默认空格数为8-len(str)%8,str表示两个 之间的

    .find(sub[,start[,end]])  int 检测sub是否包含在范围内,是返回第一个字符的索引值,否返回-1
    .format(*args, **kwargs)  str '{0} love {b}'.format('I',b='you') 在0,b位置替换对应字符串
    .format_map()  str

    类似于str.format(**mapping),除了mapping直接使用,而不复制到dict。

    这是有用的,例如mapping是一个dict子类:

    >>> class Default(dict):

        ...

        def __missing__(self, key): #调用不在字典中的key返回本身

            ...

            return key

    >>> '{name} was born in {country}'.format_map(Default(name='Guido'))

    'Guido was born in country'

    .index(sub[,start[,end]])  int 同find,不过否的话产生一个异常
    .isalnum()  bool 字符串至少有一个字符且所有字符都是字母(汉字)或数字则返回True
    .isalpha()  bool (参考上一个)所有字符都是字母(汉字)
    .isdecimal()  bool 只包含数字(Unicode, 全角)
    .isdigit()  bool 只包含数字(Unicode, 全角, bytes(b'1'), 罗马数字)
    .isidentifier()  bool

    如果字符串是根据python语言定义的有效标识符,则返回true有效标识符:

    由字母(汉字)数字下划线组成且不以数字开头

    .islower()  bool 至少包含一个小写字符, 且不包含大写字符
    .isprintable()  bool 如果字符串中的所有字符都可打印或字符串为空,则返回true
    .isnumeric()  bool 只包含数字字符
    .isspace()  bool 只包含空白符
    .istitle()  bool 所有单词大写开头其余小写(标题化)
    .isupper()  bool 至少包含一个大写字符, 且不包含小写字符
    .join(iterable)  str 以字符串为分隔符,插入到iterable中所有字符之间
    .ljust(width[, fillchar])  str 左对齐并填充空格(fillchar)到长度为width
    .lower()  str 转化大写为小写(仅A-Z)
    .lstrip([chars])  str 去掉左边的空格(chars)
    .maketrans(x, y=None, z=None)  dict

    此静态方法返回可用于str.translate()的转换表仅一个参数:字典, key(int, char)与value(char)

    两个参数: 等长字符串, 一一对应创建映射关系三个参数: z中的字符都会被映射为Nonereturn: {ASCII码:char}

    .partition(sep)  tuple 找到sep把原字符串分割成三部分(pre_sub,sep,fol_sub), 找不到返回('原字符串',' ',' ')
    .replace(old,new,[,count])  str 把old替换为new,count指定最多替换次数
    .rfind(sub[,start[,end]])  int 类似find,不过从右边开始查找
    .rindex(sub[,start[,end]]) int  类似index, 不过从右边开始
    .rjust(width[, fillchar]) str  类似ljust, 右对齐
    .rpartition(sep)  tuple 类似partition, 从右边开始
    .rsplit(sep=None, maxsplit=-1)  list 同split(区别:split从左到右,rsplit从右到左处理str)
    .rstrip()  str 删除末尾空白符
    .split(sep=None, maxsplit=-1)  list

    不带参数默认是以空格为分隔符切片字符串, 如果maxsplit参数有设置,

    则仅分割maxsplit 个字符串,返回切片后的子字符串拼接的列表

    .splitlines([keepends])  list 按照' '分割,返回一个包含各行作为元素的列表, 如果参数指定,则返回前指定行。
    .startswith(perfix[,start[,end]])  bool 检查字符串是否在范围内以prefix开头
    .strip([chars])  str 删除两边的空白符,参数可以定制删除的字符
    .swapcase()  str 翻转字符串中的大小写
    .title()  str 返回标题化的字符串
    .translate(table)  str

    根据table规则(可由str.maketrans('a','b')定制)转换字符串中的字符table:

    dict {ASCII码:char} 替换字符串中ASCII码对应的字符

    .upper()  str 转换所有小写为大写
    .zfill(width)  str 右对齐,前面填充0至长度width
  • 相关阅读:
    .Net/C# 应用程序直接读取本地 Cookies 文件(WinXP SP2 调用 API: InternetGetCookie 无果)
    wininet.dll函数库:不会过期的cookie
    WinForm中TextBox控件循环自动滚动示例
    JScript中Date.getTime转.Net中的DateTime
    js gettime c# ticks
    mysql查看整库个表详情
    rds分区实践
    mysql5.7.21源码安装
    EXPLAIN详解
    C#基础温习(4):C#中string数组和list的相互转换
  • 原文地址:https://www.cnblogs.com/P--K/p/7897009.html
Copyright © 2011-2022 走看看