python3字符串方法

>>> str='absfgsdf'
>>> type(str)
<class 'str'>
>>> 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']
>>>

'capitalize'首字母大写 'casefold'小写 'center'居中填充'count'计数 'encode'编码, 'endswith'结尾, 'expandtabs'扩展tab, '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'扩充



>>> 'asdfsd'[1]
's'
>>>

capitalize(self):
>>> s.capitalize()
'Sdfsgfdg'
>>>

casefold(self):
>>> s='DFTGFFY'
>>> s.casefold()
'dftgffy'
>>>

center(self, width, fillchar=None):
>>> s='sfsggsd'
>>> s.center(22)
'       sfsggsd        '
>>>

count(self, sub, start=None, end=None):
>>> s='sfsggsd'
>>> s.count('s')
3
>>>

encode(self, encoding='utf-8', errors='strict'):
>>> name='明宣'
>>> name.encode('gbk')
b'\xc3\xf7\xd0\xfb'
>>>
utf-8转gbk

endswith(self, suffix, start=None, end=None):
>>> s='sfsggsd'
>>> s.endswith("sd")
True
>>>

expandtabs(self, tabsize=8):
>>> ss='\tasfa'
>>> ss.expandtabs()
'        asfa'
>>>

find(self, sub, start=None, end=None):
>>> name='safadsf'
>>> name='safadsf'
>>> name.find('ds')
4
>>>

format(self, *args, **kwargs): 
>>> name='saf {0}sd{1}'
>>> name.format('aaa',666)
'saf aaasd666'
>>>
>>> name='afdsffs{name}sdf{sdf}'
>>> name.format(name='sssss',sdf=66666)
'afdsffsssssssdf66666'
>>>

format_map(self, mapping): 


index(self, sub, start=None, end=None): 
>>> name='hghghghghghghghgh'
>>> name.index('hg',1,-1)
2
>>>


isalnum(self): 
>>> '))(*)*'.isalnum()
False
>>> 'sds'.isalnum()
True
>>>

isalpha(self): 
>>> '124'.isalpha()
False
>>>

isdecimal(self): 
>>> '345'.isdecimal()
True
>>>

isdigit(self): 
>>> '5325.35'.isdigit()
False
>>> '5645'.isdigit()
True
>>>

isidentifier(self): 
>>> '12ssd'.isidentifier()
False
>>>

islower(self): 
>>> 'sHJK'.islower()
False
>>>

isnumeric(self): 
>>> 'is'.isnumeric()
False
>>>

isprintable(self): 
>>> 'sfs\n'.isprintable()
False
>>>

isspace(self): 
>>> ' df'.isspace()
False
>>>

istitle(self): 
>>> 'Jkfjs Jkfa'.istitle()
True
>>>

isupper(self): 
>>> 'JKH'.isupper()
True
>>>

join(self, iterable): 
>>> '-'.join(['s','sf','sf'])
's-sf-sf'
>>>

ljust(self, width, fillchar=None): 
>>> 'saf'.ljust(6)
'saf   '
>>>

lower(self): 
>>> 'LoWeR'.lower()
'lower'
>>>

lstrip(self, chars=None): 
>>> 'sfa  '.lstrip(' ')
'sfa '
>>>

maketrans(self, *args, **kwargs): 
>>> a='uiuyiyrfy'.maketrans('bcd','234')
>>> a
{98: 50, 99: 51, 100: 52}
>>> 'abcdefabcdef'.translate(a)
'a234efa234ef'
>>>


partition(self, sep): 
>>> 'abcdabcdabcd'.partition('bc')
('a', 'bc', 'dabcdabcd')
>>>

replace(self, old, new, count=None): 
>>> 'abcabcabc.'.replace('bc','777')
'a777a777a777.'
>>>

rfind(self, sub, start=None, end=None): 
>>> 'aaaaaa'.rfind('a',0,5)
4
>>>

rindex(self, sub, start=None, end=None): 
>>> 'aaaa'.rindex('a')
3
>>>

rjust(self, width, fillchar=None): 
>>> 'aaa'.rjust(6)
'   aaa'
>>>

rpartition(self, sep): 
>>> 'aaa'.rpartition('a')
('aa', 'a', '')
>>>

rsplit(self, sep=None, maxsplit=-1): 
['a', 'sdf', 'sdf', 'sdaf']
>>>

rstrip(self, chars=None): 
>>> 'asasasasasasasasa'.rstrip('a')
'asasasasasasasas'
>>>

split(self, sep=None, maxsplit=-1): 
>>> 'ss  af  ggs  sf s\n'.split()
['ss', 'af', 'ggs', 'sf', 's']
>>>

splitlines(self, keepends=None): 
>>> 'a\n\n\na'.splitlines()
['a', '', '', 'a']
>>>

startswith(self, prefix, start=None, end=None): 
>>> 'abcdef'.startswith('ab')
True
>>>

strip(self, chars=None): 
>>> 'a1a1a1a1'.strip('a')
'1a1a1a1'
>>>

swapcase(self): 
>>> 'aaaAAA'.swapcase()
'AAAaaa'
>>>

title(self): 
>>> 'aaa aaa aaa'.title()
'Aaa Aaa Aaa'
>>>

translate(self, table): 
>>> a='uiuyiyrfy'.maketrans('bcd','234')
>>> a
{98: 50, 99: 51, 100: 52}
>>> 'abcdefabcdef'.translate(a)
'a234efa234ef'
>>>

upper(self): 
>>> 'aaaAAAaaa'.upper()
'AAAAAAAAA'
>>>

zfill(self, width): 
>>> 'aaa'.zfill(5)
'00aaa'
>>>

 

posted @ 2017-09-13 22:38  明宣  阅读(194)  评论(0编辑  收藏  举报