Reputation: 33
I don't know how to do something like this in python:
I have a string s = 'windows 95 is not a windows nt operating system'
, and I want to get string that contains 'nt' and a number of nearby letters, including spaces.
Expected output for 7 nearby letters:
'indows nt operat'
If it is impossible, then is it possible to get the index of a string I want to find like this:
>>> s = 'windows xp horray'
>>> stringtofind = 'hor'
Expected output:
11, 12, 13
Where I only want to get 11
because it is the start."
Is this possible?
Upvotes: 2
Views: 118
Reputation: 523
I would suggest using find()
to get the index of the substring in the main string and then get the part that you want:
s = 'windows 95 is not a windows nt operating system'
substr = 'nt'
index = s.find(substr)
result = s[index-7: index+len(substr)+7]
print(result)
Upvotes: 0
Reputation: 82785
Use str.find
with slicing.
Ex:
s = 'windows 95 is not a windows nt operating system'
to_find = 'nt'
print(s[s.find(to_find)-7:s.find(to_find)+ 7])
# --> indows nt oper
Upvotes: 2