python3字符串方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
>>>

 

以上是关于python3字符串方法的主要内容,如果未能解决你的问题,请参考以下文章

scrapy按顺序启动多个爬虫代码片段(python3)

10个JavaScript代码片段,使你更加容易前端开发。

10个JavaScript代码片段,使你更加容易前端开发。

python常用代码片段总结

python3 字符串的常用方法总结

片段中ListView的setOnItemClickListener