美文网首页
字符串str

字符串str

作者: littlefogcat | 来源:发表于2017-08-02 03:03 被阅读0次

    >>> dir(str)
    ['add', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

    dir

    字符串的函数非常多啊。。挑几个常用的;
    s.capitalize() 首字母大写
    s.count(s1) 计算s1在s中出现的次数
    s.find(s1[,start[,end]]) 查找s1在s中第一次出现的位置,找不到返回-1 可选:start,end:查找的起始,结束位置
    s.format() 格式化字符串,例:

    '{0},{1}'.format('hello',4)  #输出:hello,4
    '{0:.2f}'.format(3.14159)  #精确到小数点后2位,输出:3.14
    

    s.index(s1[,start[,end]]) 同find。区别是找不到抛出异常
    s.isalnum() 介绍说是所有字符都是字母或数字则返回True,实际上汉字也返回True

    感觉这样做没什么意义,不如直接查python教程

    相关文章

      网友评论

          本文标题:字符串str

          本文链接:https://www.haomeiwen.com/subject/sutelxtx.html