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
  • 112,593
  • 23
  • 157
  • 280
stumped
  • 3,107
  • 7
  • 37
  • 70

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
  • 66,333
  • 7
  • 57
  • 95
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
  • 56,204
  • 11
  • 116
  • 135
0

Duplicate of How do I trim whitespace with Python?

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

soundstripe
  • 1,334
  • 9
  • 17