• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python中splitlines()方法的使用简介

python 搞代码 4年前 (2022-01-07) 13次浏览 已收录 0个评论

这篇文章主要介绍了Python中splitlines()方法的使用简介,是Python入门中的基础知识,需要的朋友可以参考下

 splitlines()方法返回一个字符串的所有行,可选包括换行符列表(如果num提供,则为true)
语法

以下是splitlines()方法的语法:

 str.splitlines( num=string.count('\n')) 

参数

  •     num — 这是任何数,如果存在它会被认为换行需要被包括行数。

返回值

如果找到匹配的字符串此方法返回true,否则为false。
例子

下面的例子显示splitlines()方法的使用。

 #!/usr/bin/python str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"; print str.splitlines( ); print str.splitlines( 0 ); print str.splitlines( 3 ); print str.splitlines( 4 ); print str.splitlines( 5 ); 

当我们运行上面的程序,它会产生以下结果:

 ['Line1-a b c d e f', 'Line2- a b c'<em style="color:transparent">来源[email protected]搞@^&代*@码)网</em>, '', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] 

以上就是Python中splitlines()方法的使用简介的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python中splitlines()方法的使用简介

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址