【每天1分钟】PYTHON基础之数据类型-字符串(修改内容)
- expandtabs()
把字符串中的 tab 符号(’\t’)转为空格,tab 符号(’\t’)默认的空格数是 8
>>> st = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
>>> print(st)
username email password
laiying ying@q.com 123
laiying ying@q.com 123
laiying ying@q.com 123
>>> print(st.expandtabs())
username email password
laiying ying@q.com 123
laiying ying@q.com 123
laiying ying@q.com 123
>>> print(st.expandtabs(20))
username email password
laiying ying@q.com 123
laiying ying@q.com 123
laiying ying@q.com 123
>>>
- replace()
将字符串的某些字符替换成其他字符
S.replace(old, new[, count])
old:将要被我们替换的旧的字符串
new:新字符串,用来替换旧的字符串(替换一次或者多次old)
count:用来替换的次数,这里有两种:(1)当不将count参数传入时,默认将所有old字符或者字符串替换为new字符或者字符串;(2)当我们将count参数传入后,则将旧字符串替换为新字符串不超过count次,多余的则不进行替换
>>> st = "aabbaaccaaddaaee"
>>> st.replace("a","1")
'11bb11cc11dd11ee'
>>> st.replace("aa","1")
'1bb1cc1dd1ee'
>>> st.replace("aa","1",2)
'1bb1ccaaddaaee'
>>> st.replace("aa","1",100)
'1bb1cc1dd1ee'
>>>
- split()
拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)
S.split(sep=None, maxsplit=-1)
sep:分隔符
maxsplit: 分割次数
>>> st = "https://mp.youkuaiyun.com/mdeditor/96460910"
>>> st.split(".")
['https://mp', 'csdn', 'net/mdeditor/96460910']
>>> st.split("/")
['https:', '', 'mp.youkuaiyun.com', 'mdeditor', '96460910']
>>> st.split("/",3)
['https:', '', 'mp.youkuaiyun.com', 'mdeditor/96460910']
>>> st.split("//")
['https:', 'mp.youkuaiyun.com/mdeditor/96460910']
>>>
S.rsplit(sep=None, maxsplit=-1)
sep:分隔符
maxsplit: 分割次数
>>> st = "https://mp.youkuaiyun.com/mdeditor/96460910"
>>> st.rsplit(".")
['https://mp', 'csdn', 'net/mdeditor/96460910']
>>> st.rsplit("/")
['https:', '', 'mp.youkuaiyun.com', 'mdeditor', '96460910']
>>> st.rsplit("/",1)
['https://mp.youkuaiyun.com/mdeditor', '96460910']
>>> st.rsplit("/",100)
['https:', '', 'mp.youkuaiyun.com', 'mdeditor', '96460910']
>>>
- splitlines()
按照行界符(’\r’, ‘\r\n’, \n’等)分隔,返回一个包含各行作为元素的列表,默认不包含行界符。
能被识别的行界符:
行界符 | 描述 |
---|
\n | Line Feed 换行 |
\r | Carriage Return 回车 |
\r\n | Carriage Return + Line Feed 回车+换行 |
\v or \x0b | Line Tabulation |
\f or \x0c | Form Feed 换页 |
\x1c | File Separator 文件分隔符 |
\x1d | Group Separator 组分隔符 |
\x1e | Record Separator 记录分隔符号 |
\x85 | Next Line (C1 Control Code) |
\u2028 | Line Separator 行分隔符 |
\u2029 | Paragraph Separator 段落分隔符号 |
S.splitlines([keepends])
keepends -- 在输出结果里是否去掉行界符('\r', '\r\n', \n'等),默认为 False,不包含行界符,如果为 True,则保留行界符。
>>> st = "HOW\nARE\rYOU"
>>> st.splitlines()
['HOW', 'ARE', 'YOU']
>>> st.splitlines(False)
['HOW', 'ARE', 'YOU']
>>> st.splitlines(True)
['HOW\n', 'ARE\r', 'YOU']
>>> st.splitlines(1)
['HOW\n', 'ARE\r', 'YOU']
>>> st.splitlines(2)
['HOW\n', 'ARE\r', 'YOU']
>>> st.splitlines(0)
['HOW', 'ARE', 'YOU']
>>>
- partition
根据指定的分隔符将字符串进行分割
S.partition(sep) -> (head, sep, tail)
sep:分隔符
>>> st = "https://www.youkuaiyun.com/"
>>> st.partition(".")
('https://www', '.', 'youkuaiyun.com/')
>>> st.partition("/")
('https:', '/', '/www.youkuaiyun.com/')
>>>
- rpartition()
类似于 partition() 方法,只是该方法是从目标字符串的末尾也就是右边开始搜索分割符
S.rpartition(sep) -> (head, sep, tail)
sep:分隔符
>>> st = "https://www.youkuaiyun.com/"
>>> st.rpartition(".")
('https://www.csdn', '.', 'net/')
>>> st.rpartition("/")
('https://www.youkuaiyun.com', '/', '')
>>>
- join(iter)
连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
S.join(iterable) -> str
iterable:可迭代对象
>>> st = "hello youkuaiyun.com"
>>> ".".join(st)
'h.e.l.l.o. .c.s.d.n...n.e.t'
>>> "-".join(st)
'h-e-l-l-o- -c-s-d-n-.-n-e-t'
>>> "++".join(st)
'h++e++l++l++o++ ++c++s++d++n++.++n++e++t'
>>>