python学习之str

来源:互联网 发布:2017淘宝双11红包 编辑:程序博客网 时间:2024/06/05 19:52
>>> dir(str)['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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']>>> name='jofdmsdfjso'>>> name'jofdmsdfjso'>>> name.islower()True>>> name.isupper()False>>> num='32'>>> num.isdigit()True>>> num.isdecimal()True>>> name.find('so')9>>> name.index('so')9>>> name.index('4')Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found>>> name.find('4')-1>>> name.replace('j','J')'JofdmsdfJso'>>> num.zfill(10)'0000000032'>>> name.upper()'JOFDMSDFJSO'>>> name'jofdmsdfjso'>>>



# name=str('d')name='{0} and {1} is together'rs=name.format('liyu','shatoutou')print(rs)name='{n1} and {n2} is together'rs=name.format(n1='liyu1',n2='shatoutou1')print(rs)str='''line1line2line3'''# rs=str.split('\n')rs=str.splitlines()print(rs)keys='abcdef'vals='123456'mytran=''.maketrans(keys,vals)str='abc is not def'rs=str.translate(mytran)print(rs)name='liyuissb'rs=name.partition('is')print(rs)l=['aa','bb','cc']rs='_'.join(l)print(rs)name='li\tyu'rs=name.expandtabs(1)print(rs)


0 0