#Python中截取字符串

21.(str).split()
split(str="",num)
以str为分隔符截取字符串,则仅截取num 个字符串

str5 = "sunck*is**a*****good man"
print(str5.split("*",4))
#(输出)['sunck', 'is', '', 'a', '****good man'] 

22.(str).splitlines()
splitlines([keepends]) 按行(’\r’,’\r\n’,’\n’ )分隔,返回
keepends == True 会保留换行符

str40 = """sunck is a good man 
sunck is a beautiful man 
sunck is a nice man 
"""
print(str40.splitlines())
#(输出)['sunck is a good man ', 'sunck is a beautiful man ', 'sunck is a nice man ']

print(str40.splitlines(True))
#(输出)['sunck is a good man \n', 'sunck is a beautiful man \n', 'sunck is a nice man \n']

猜你喜欢

转载自blog.csdn.net/weixin_43097301/article/details/82954462