Home:ALL Converter>Truncate a string around a word found in that string in Python

Truncate a string around a word found in that string in Python

Ask Time:2020-09-19T04:45:33         Author:Nitesh Joshi

Json Formatter

I want to find a word in a string and then truncate the python string around that word.

Example: str1 = "I want to try and select some specific thing in this world. Can you please help me do that"

Now I want to find the word specific in the string and then truncate the string from front and end to say 15 chars around that word.

So the answer would be something like: "nd select some specific thing in this "

This is basically 15 characters left and right from "specific".

Thanks in advance

Author:Nitesh Joshi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63962630/truncate-a-string-around-a-word-found-in-that-string-in-python
yy