Th0m45 Th0m45 - 3 months ago 20
Python Question

Python split by "%NumberNumber"

I have the following strings for example

str = "Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully"


What i am trying to achieve is:

array = str.split("%NumberNumber")
for unit in array:
print(unit)


Console:

Thisisaexample123
321Lorem
Ipsum
Bad123Example
Hopefully


Hm... maybe using Regex?

Answer Source

just use re.split using %\d\d as the splitting regex. (percent & 2 digits)

import re

print("\n".join(re.split("%\d\d","Thisisaexample123%20321Lorem%29Ipsum%28Bad123Example%20Hopefully")))

result:

Thisisaexample123
321Lorem
Ipsum
Bad123Example
Hopefully

(aside: don't use str as a variable name as it's the built-in string object name)