1. 程式人生 > >python3字符串方法

python3字符串方法

ndt rjust new span rst iss color dfs con

>>> 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 >>>

python3字符串方法