Reputation: 1806
I have 2 strings
a = "abc feat. def"
b = "abc Feat. def"
I want to retrieve the string before the word feat.
or Feat.
This is what I'm doing,
a.split("feat.", 1)[0].rstrip()
This returns abc
. But how can I perform a case insensitive search using split delimiter?
This is what I've tried so far
b.split("feat." or "Feat.", 1)[0].rstrip()
Output - abc Feat. def
b.split("feat." and "Feat.", 1)[0].rstrip()
Output - abc
a.split("feat." and "Feat.", 1)[0].rstrip()
Output - abc feat. def
.
a.split("feat." or "Feat.", 1)[0].rstrip()
Output - abc
Why is this difference with and
and or
in both the cases?
Upvotes: 14
Views: 8122
Reputation: 7129
a[0:a.lower().find("feat.")].rstrip()
would do.
and
ing
"string1" and "string2" and ... and "stringN"
returns the the last string.
or
ing
"string1" or "string2" or ... or "stringN"
would return the first string.
Upvotes: 10
Reputation: 12921
You should use regex:
re.split('\s*[Ff]eat\.', a)
and
and or
do some boolean judgement.
"feat." or "Feat." -> "feat." if "feat." else "Feat." -> "feat."
"feat." and "Feat." -> "Feat." if "feat." else "feat." -> "Feat."
Upvotes: 1
Reputation: 336178
Use a regex instead:
>>> import re
>>> regex = re.compile(r"\s*feat\.\s*", flags=re.I)
>>> regex.split("abc feat. def")
['abc', 'def']
>>> regex.split("abc Feat. def")
['abc', 'def']
or, if you don't want to allow FEAT.
or fEAT.
(which this regex would):
>>> regex = re.compile(r"\s*[Ff]eat\.\s*")
Upvotes: 17