Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get nth line of string in python

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 using stdlib/builtin functions? I prefer a solution in Python 3, but Python 2 is also fine.

like image 479
Ramchandra Apte Avatar asked Jul 15 '12 12:07

Ramchandra Apte


1 Answers

Try the following:

s = "line1\nline2\nline3"
print s.splitlines()[2]
like image 144
Mark Longair Avatar answered Oct 03 '22 01:10

Mark Longair