Rohan Nayani
Rohan Nayani

Reputation: 43

Split Big String by Specific Word In Python

I want to split a big string by word and that word is repeating in that big string.

Example what i expect : What I expect

We have tried to split a code, please check below

string.split("RFF+AAJ:")

So we need a bunch of list that i have described in my above screenshot.

Upvotes: 1

Views: 6095

Answers (2)

Rahul charan
Rahul charan

Reputation: 837

You can get your result with the help of regex :-

import re
string = 'helloisworldisbyeishi'
re.split('(is)', string)  # Splitting from 'is'

Output

['hello', 'is', 'world', 'is', 'bye', 'is', 'hi']

I hope it may help you.

Upvotes: 4

T. Feix
T. Feix

Reputation: 179

split returns one single list with the complete string in it ( it is just split in parts ). So the list here contains the part before the first "RFF+AAJ:", then the part between the two "RFF+AAJ:"s and the last part, after the second "RFF+AAJ:". If you want to have three differrent lists use:

all = string.split("RFF+AAJ:")
first = all[0]
second = all[1]
third = all[2]

And the elements will be stored in first, second and third. If you want to create lists, use first = list(first) # and so on. Hope that helped.

Upvotes: 1

Related Questions