I have a long string, and I want to break it into smaller stinger whenever a certain pattern showed up: (in below case 123 my)
my_str = '123 my string is long 123 my string is very long 123 my string is so long'
I want the result to be:
result = ['123 my string is long ', '123 my string is very long ', '123 my string is so long ']
Length of string is unknown. and I don't want to remove anything from the main string.
Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.
The splitter can be a single character, another string, or a regular expression. After splitting the string into multiple substrings, the split() method puts them in an array and returns it. It doesn't make any modifications to the original string.
You can also use a look ahead regex:
import re
re.split(r'.(?=123 my)', my_str)
=>
['123 my string is long',
'123 my string is very long',
'123 my string is so long']
You can split on the delimiter and then add it back in with a list comprehension:
my_str = '123 my string is long 123 my string is very long 123 my string is so long'
delimiter = '123 my'
result = ['{}{}'.format(delimiter, s) for s in my_str.split(delimiter) if s]
print(result)
Output
['123 my string is long ', '123 my string is very long ', '123 my string is so long']
I don't know where the trailing space in the last list item comes from in your desired output, it's not in the original string and so should be absent in the result.
Note that this only works if the delimiter begins at the start of the string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With