【每天1分钟】PYTHON基础之数据类型-字符串(修改内容)

本文介绍了Python中字符串的各种操作方法,包括使用expandtabs()调整制表符宽度、利用replace()进行字符替换、通过split()和rsplit()进行字符串切割、采用splitlines()按行分割字符串、运用partition()和rpartition()进行分隔符分割以及使用join()连接字符串数组。

【每天1分钟】PYTHON基础之数据类型-字符串(修改内容)

  • expandtabs()
    把字符串中的 tab 符号(’\t’)转为空格,tab 符号(’\t’)默认的空格数是 8
# S.expandtabs([tabsize=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']
>>> 
  • rsplit()
    同split,但从右到左
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’等)分隔,返回一个包含各行作为元素的列表,默认不包含行界符。
    能被识别的行界符:
行界符描述
\nLine Feed 换行
\rCarriage Return 回车
\r\nCarriage Return + Line Feed 回车+换行
\v or \x0bLine Tabulation
\f or \x0cForm Feed 换页
\x1cFile Separator 文件分隔符
\x1dGroup Separator 组分隔符
\x1eRecord Separator 记录分隔符号
\x85Next Line (C1 Control Code)
\u2028Line Separator 行分隔符
\u2029Paragraph 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'
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值