get nth line of string in python

后端 未结 9 1774
萌比男神i
萌比男神i 2021-01-02 08:50

How can you get the nth line of a string in Python 3? For example

getline(\"line1\\nline2\\nline3\",3)

Is there any way to do this

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 09:27

    Try the following:

    s = "line1\nline2\nline3"
    print s.splitlines()[2]
    

提交回复
热议问题