How to get a string after a specific substring?

havox picture havox · Sep 24, 2012 · Viewed 514.1k times · Source

How can I get a string after a specific substring?

For example, I want to get the string after "world" in my_string="hello python world , I'm a beginner " (which in this case it is:, I'm a beginner)

Answer

Joran Beasley picture Joran Beasley · Sep 24, 2012

The easiest way is probably just to split on your target word

my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1] 

split takes the word(or character) to split on and optionally a limit to the number of splits.

In this example split on "world" and limit it to only one split.