博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3 字符串属性(四)
阅读量:4982 次
发布时间:2019-06-12

本文共 2406 字,大约阅读时间需要 8 分钟。

1、

S.partition(sep) -> (head, sep, tail)

Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it. If the separator is not
found, return S and two empty strings.

分割作用,参数为分割字符,分为三部分,(参数前,参数,参数后);如果参数没有找到,返回原字符串和两个空字符串。参数有多个,以第一个为准。S.partition(sep) -> (head, sep, tail)   以最后一个参数为准。

 

1 >>> a 2 'acbsdwf124' 3 >>> a.partition('d') 4 ('acbs', 'd', 'wf124') 5 >>> a.partition('i') 6 ('acbsdwf124', '', '') 7 >>> a.partition('sd') 8 ('acb', 'sd', 'wf124') 9 >>> a='hello world hello huhu !'10 >>> a.partition('hello')11 ('', 'hello', ' world hello huhu !')

 

2、

S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.

替换

1 >>> a='1a2a3a4a5a'2 >>> a.replace('a','b')3 '1b2b3b4b5b4 >>> a.replace('a','b',3)5 '0b1b2b3a4a5a    #替换前三个

 

3、

S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.

S.rsplit(sep=None, maxsplit=-1) -> list of strings  第二个参数,从右侧开始划分。

文本解析,默认为空格,空格将被移除。返回字符串列表。

1 >>> a='hello world ,huhu !' 2 >>> a.split() 3 ['hello', 'world', ',huhu', '!'] 4 >>> a.split(',') 5 ['hello world ', 'huhu !'] 6 >>> a='hello:world:huhu' 7 >>> a.split(':',2) 8 ['hello', 'world', 'huhu'] 9 >>> a.split(':',1)10 ['hello', 'world:huhu']  #从左侧划分。

S.splitlines([keepends]) -> list of strings

Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true

1 >>> a="""hello world!2 ... second line3 ... splitlins test4 ... """5 >>> a.splitlines()6 ['hello world!', 'second line', 'splitlins test']7 >>> a.splitlines(True) #保留行分割符8 ['hello world!\n', 'second line\n', 'splitlins test\n']

 

4、

S.swapcase() -> str

Return a copy of S with uppercase characters converted to lowercase
and vice versa.

字母大小写转换

1 >>> a='ABCDefgh'2 >>> a.swapcase()3 'abcdEFGH'

 

5、

S.zfill(width) -> str

Pad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.

 

给定长度,左侧添零补充

1 >>> a2 'ABCDefgh'3 >>> a.zfill(10)4 '00ABCDefgh'

 

转载于:https://www.cnblogs.com/hb91/p/5267337.html

你可能感兴趣的文章
(树)根据排序数组或者排序链表重新构建BST树
查看>>
hunnu--11548--找啊找啊找朋友
查看>>
敲代码非常难之去除字符串的空白字符
查看>>
《人月神话 》读后感
查看>>
map,arrayList,jsonobject
查看>>
巨蟒python全栈开发django4:url反向解析图解&&模板渲染
查看>>
Windows 下 Python easy_install 的安装
查看>>
golang 多线程查找文件内容
查看>>
用拓扑图展现层级和组织关系(一)
查看>>
[转]13 Hours: The Secret Soldiers of Benghazi
查看>>
阿里云oss,简单上传
查看>>
软件测试2019:第四次作业
查看>>
create xmlhttprequest
查看>>
自动化学习路线
查看>>
iTOP-4418开发板Android 5.1/4.4丨Linux + Qt5.7丨Ubuntu12.04系统
查看>>
codeforces 609C Load Balancing
查看>>
Java类加载器的工作原理
查看>>
存储过程--InOut
查看>>
【十次方基础教程(后台)】3、搭建父工程
查看>>
【转载】C#将图片转换为二进制流调用
查看>>