Check if parameter in string is followed by a specific letter – python
Question:
I found a similar answer for this but only in java. Here is an example of my code at the moment:
def find_x(phrase, target):
i=phrase.find(target)
if 'x' in phrase[i+1]:
return True
else:
return False
I then realized in the case the phrase is ‘Texas’ and the target is ‘Te’ the find function will only give the index of the first letter of the first occurrence of the target. Therefore when it checks if the next letter is ‘x’ it will be part of the target returning False instead of True. Let me know if there is a way to get around this issue, thanks!
Answers:
Building on the suggestion in the comments to check for target + 'x' in phrase
, you could just make that check and then also check that it’s in the same spot as the first occurrence of target
:
>>> def find_x(phrase, target):
... return target + 'x' in phrase and phrase.find(target) == phrase.find(target + 'x')
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False
The option of explicitly looking for the first occurrence of target
and then checking the character immediately after it is more efficient, since it only requires one scan through phrase
. You just want to catch the potential IndexError
or ValueError
and return False
instead of raising.
>>> def find_x(phrase, target):
... try:
... return phrase[phrase.index(target) + len(target)] == 'x'
... except (IndexError, ValueError):
... return False
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False
>>> find_x('Te', 'Te') # this is the case that'll raise IndexError
False
>> find_x('Texas', 'Tee') # this is the case that'll raise ValueError
False
I found a similar answer for this but only in java. Here is an example of my code at the moment:
def find_x(phrase, target):
i=phrase.find(target)
if 'x' in phrase[i+1]:
return True
else:
return False
I then realized in the case the phrase is ‘Texas’ and the target is ‘Te’ the find function will only give the index of the first letter of the first occurrence of the target. Therefore when it checks if the next letter is ‘x’ it will be part of the target returning False instead of True. Let me know if there is a way to get around this issue, thanks!
Building on the suggestion in the comments to check for target + 'x' in phrase
, you could just make that check and then also check that it’s in the same spot as the first occurrence of target
:
>>> def find_x(phrase, target):
... return target + 'x' in phrase and phrase.find(target) == phrase.find(target + 'x')
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False
The option of explicitly looking for the first occurrence of target
and then checking the character immediately after it is more efficient, since it only requires one scan through phrase
. You just want to catch the potential IndexError
or ValueError
and return False
instead of raising.
>>> def find_x(phrase, target):
... try:
... return phrase[phrase.index(target) + len(target)] == 'x'
... except (IndexError, ValueError):
... return False
...
>>> find_x('Texas', 'Te')
True
>>> find_x('TeTexas', 'Te')
False
>>> find_x('Te', 'Te') # this is the case that'll raise IndexError
False
>> find_x('Texas', 'Tee') # this is the case that'll raise ValueError
False