Home > Mobile >  How to remove line break from start of a string in python?
How to remove line break from start of a string in python?

Time:03-11

I have a string like this

my_str = "\n\n\n\n\n\nhey i am some content \n\n i am some other content"

I want to remove the first line breaks from my_str.

my_str = "hey i am some content \n\n i am some other content"

How I can do this in Python?

CodePudding user response:

Use .lstrip(). A regular expression isn't necessary here.

my_str = "\n\n\n\n\n\nhey i am some content \n\n i am some other content"

result = my_str.lstrip()
print(result)

This outputs:

hey i am some content 

 i am some other content

CodePudding user response:

you can use re.sub

import re
my_str = "\n\n\n\n\n\nhey i am some content \n\n i am some other content"
re_p=r'^\n '
print(re.sub(re_p,'',my_str))
#'hey i am some content \n\n i am some other content'
  • Related