-1

I have a string in python.

str1 = "hello\n1hello123\n2yahoo"

I would like to split this with \n[integer value] to get a list that looks like: [hello, hello123, yahoo]

Can anyone please help?

2 Answers2

0

As someone who goes too far in avoiding regular expressions (avoiding them whenever possible rather than simply avoiding them when they are inappropriate), I would lean towards splitting on \n and processing the resulting list element by element:

from string import digits

result = [x.lstrip(digits) for x in str1.split("\n")]

If you are less regex-averse than I, and as recommended in the comments,

from re import split
from string import digits

results = split(f'\n[{digits}]*', str1)
chepner
  • 497,756
  • 71
  • 530
  • 681
0

regular expression package

enter code here
str1 = "hello\n1hello123\n2yahoo"
import re
print(re.split(r"\n[1-9]", str1))