Latest web development tutorials

Python3 splitlines()方法

Python3 字符串 Python3字符串


描述

Python splitlines() 按照行分隔,返回一個包含各行作為元素的列表,如果num 指定則僅切片num 個行.

語法

splitlines()方法語法:

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

參數

  • num -- 分割行的次數。

返回值

返回一個包含各行作為元素的列表。

實例

以下實例展示了splitlines()函數的使用方法:

#!/usr/bin/python3

str = "this is \nstring example....\nwow!!!"
print (str.splitlines( ))

以上實例輸出結果如下:

['this is ', 'string example....', 'wow!!!']

Python3 字符串 Python3字符串