Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23305

How to remove everything in a string before first occurrence of pattern (Python) [duplicate]

$
0
0

I have a string that looks like the following:

sample_string = 'Hello, my name is Bob.\nI like sparkling water.\nMy favorite flavor is mango.\n Goodbye.'

I want to do two things:

  1. Edit the string in a way that removes all characters before the first occurrence of '\n'
  2. Remove all occurrences of '\n'

I've got #2 sorted out just fine:

sample_string.replace('\n','')

But, I'm not sure how to structure a regex expression to pinpoint the first \n and remove all characters before it.

The final string should look like:

final_string = 'I like sparkling water. My favorite flavor is mango. Goodbye.'

Viewing all articles
Browse latest Browse all 23305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>