Given a multiple line variables
var="""this is line 1
this is line 2
this is line 3
this is line 4"""
How can I assign to a variable a specific line, say line 2?
>>> var="""this is line 1
... this is line 2
... this is line 3
... this is line 4"""
>>>
>>> line3 = var.splitlines()[3 - 1]
>>> line3
'this is line 3'
str.splitlines splits your multiline-string into distinct lines. line n will be at index n - 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With