Reputation: 421
Looking to put together a simple python function that checks the following
Given two strings, return True if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive").
end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True
Upvotes: 1
Views: 1079
Reputation: 49318
You can use OOP if you want, subclassing str
and writing an appropriate method:
>>> class String(str):
... def isend(self, other):
... self = self.lower()
... other = other.lower()
... return self.endswith(other) or other.endswith(self)
...
>>> a = String('HiAbC')
>>> b = String('Bc')
>>> a.isend(b)
True
>>> b.isend(a)
True
>>> c = String('Hello')
>>> d = String('el')
>>> c.isend(d)
False
>>> d.isend(c)
False
Upvotes: 0
Reputation: 1323
You could use slicing.
>>> s1 = "Hiabc"
>>> s2 = "abc"
>>> l = len(s2)
>>> s2 == s1[-l:]
True
>>> s2 = "aBc"
>>> s2 == s1[-l:]
False
You can read more about string and slicing here.
Slicing means to get part of the string based on passed index
. General construction look like this: string[starting_index:ending_index]
.
Let's take a look at this:
string1 = "doggy"
string1[0:1] -> "d"
string1[0:2] -> "do"
string1[0:] = string[:] = "doggy" (from zero to end)
You can also pass index lesser than 0:
string1[-1:] (from -1 to end) -> "y"
So in the code above calling s1[-l:]
means starting position is the length of s2
sentece. In this case s1[-l:] = 'abc' = s1[-3:]
. Because lenght of s2
is 3.
Upvotes: 0
Reputation: 30258
Here's another approach using zip, which avoids having to do multiple passes of endswith()
, re.search()
or slicing. It iterates the 2 strings in reverse and returns True if all the letters are equal up to the exhaustion of one of the strings:
def end_other(s1, s2):
return all(a == b for a, b in zip(reversed(s1.lower()), reversed(s2.lower())))
In Python2 you can use itertools.izip()
for a marginal space improvement:
>>> end_other('Hiabc', 'abc')
True
>>> end_other('AbC', 'HiaBc')
True
>>> end_other('abc', 'abXabc')
True
>>> end_other('cbc', 'abXabc')
False
Upvotes: 1
Reputation: 699
Or if you need to "create" your own function:
def check(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
check = True
# If string 1 is bigger then string 2:
if( len(str1) > len(str2) ):
# For each character of string 2
for i in range(len(str2)):
# Compare the character i of string 2 with the character at the end of string 1, keeping the order
if str2[i] != str1[-(len(str2)-i)]:
check = False
# If string 2 is bigger then string 1:
else:
# For each character of string 1
for i in range(len(str1)):
# Compare the character i of string 1 with the character at the end of string 2, keeping the order
if str1[i] != str2[-(len(str1)-i)]:
check = False
return check
So, basically, if string1 = "ABCD"
and string2 = "CD"
, it will check character 0
of string2
with 2
of string1
and 1
of string2
with 3
of string1
Upvotes: 1
Reputation: 3036
You can use regex
def end_other(s1,s2):
return bool(re.search(s1+'$',s2,re.I)) or bool(re.search(s2+'$',s1,re.I))
Upvotes: 1
Reputation: 11590
Try with
def end_other(s1, s2):
s1 = s1.lower()
s2 = s2.lower()
return s1.endswith(s2) or s2.endswith(s1)
Upvotes: 4