Best way to remove all characters of a string until new line character is met python?
str = 'fakeline\nfirstline\nsecondline\nthirdline'
into
str = 'firstline\nsecondline\nthirdline'
Best way to remove all characters of a string until new line character is met python?
str = 'fakeline\nfirstline\nsecondline\nthirdline'
into
str = 'firstline\nsecondline\nthirdline'
Get the index of the newline and use it to slice the string:
>>> s = 'fakeline\nfirstline\nsecondline\nthirdline'
>>> s[s.index('\n')+1:] # add 1 to get the character after the newline
'firstline\nsecondline\nthirdline'
Also, don't name your string str
as it shadows the built in str
function.
Edit:
Another way (from Valentin Lorentz's comment):
s.split('\n', 1)[1]
I like this better than my answer. It's splits the string just once and grabs the latter half of the split.
str.split("\n") gives a list of all the newline delimited segments. You can simply append the ones you want with + afterwards. For your case, you can use a slice
newstr = "".join(str.split("\n")[1::])