Sometimes, we want to get a string after a specific substring with Python.
In this article, we’ll look at how to get a string after a specific substring with Python.
How to get a string after a specific substring with Python?
To get a string after a specific substring with Python, we can use the split
method.
For instance, we write
my_string = "hello world , i'm a beginner "
s = my_string.split("world", 1)[1]
to call my_string/split
with the string that we want to split by and the number that limits the number of splits.
As a result, we call split
to split the string once with 'world'
as the separator.
And we use [1]
to get the 2nd entry from the returned string list.
Conclusion
To get a string after a specific substring with Python, we can use the split
method.