Reputation: 4110
I have strings like:
a = "currency is like gbp"
a= "currency blah blah euro"
a= "currency is equivalent to usd" .....
I want to substring or slice the above string wherever I found any of "gbp" , "euro" or "usd".
Not Working:
i = a.find("gbp") or a.find("euro") or a.find("usd")
a = a[i:]
Can do:
x = a.find('gbp')
y = a.find('euro')
z = a.find('usd')
But then I need to check which of them is greater than -1 and use that variable to slice the string which will be too much code.
Also, in my original example I have 10+ currencies so want a scalable solution.
Summary:
Want to slice/substring the main sentence from any of the words found till the end
Upvotes: 1
Views: 370
Reputation: 26057
Use regex for such purposes:
import re
a = "currency is like gbp currency"
print(re.findall(r'((?:gbp|euro|usd).*)', a))
# ['gbp currency']
Upvotes: 1
Reputation: 2980
You could try something like:
currency_array = ['gbp', 'euro', 'usd']
index = max(a.find(currency) for currency in currency_array)
print(a[index:])
Upvotes: 1