0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
stumped
  • 3,235
  • 7
  • 43
  • 76

3 Answers3

0

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.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

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'
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

Duplicate of How do I trim whitespace with Python?

But basically: string = string.lstrip('\n')

soundstripe
  • 1,454
  • 11
  • 19