Splitting strings without losing the delimiter

What I want to do is split a string such as >m27348020>m8918930

into a list in this format['>m27348020', '>m8911830]

Is there a way to do this using re.split? Splitting will happen with the> symbol.

+3


source to share


2 answers


Instead of splitting, you can easily do



import re
x=">m27348020>m8918930"
print re.findall(r">[^>]*",x)

      

+2


source


You can just split the string at a given delimiter, and then just concatenate the delimiter at the start of each delimiter.



separator = ">"

line = ">m27348020>m8918930"

print [separator+i for i in line.split(separator) if len(i)>0]

>>> ['>m27348020', '>m8918930']

      

+1


source







All Articles