I've tried
if string[0] == "\n":
string = string[1:]
I don't think strip()
and rtrim()
is what I want because they'd either remove all occurrences of \n
or only at the end of the string.
You can use re.sub
:
import re
string = "\n\nHello"
new_string = re.sub("\n+", '', string)
Output:
'Hello'
This will work for any number of \n
at the beginning of a string.
You want str.lstrip()
. Exact same as strip()
except it strips from the left only, not both directions.
>>> s = '\n\nhi\n'
>>> s.lstrip('\n')
'hi\n'
Duplicate of How do I trim whitespace with Python?
But basically: string = string.lstrip('\n')