apologies to posting the question, as has been answered in other questions as well. However, couldn’t figure out what’s wrong with this solution. The question requires to find the lower-cased characters bordered by 3 upper-cased characters on each side. The code i’ve writting:
q = '' for i in range(4,len(x)-4): if x[i].islower() and x[i-4].islower() and x[i+4].islower() and x[i-3:i].isupper() and x[i+1:i+4].isupper() : q+=x[i] print(q)
The string i’m getting is
‘lgvcaaginbkvsoezhtlnldslyitlooqfgiksudtm’ vs ‘linkedlist’
Thanks for the help.
Edit: for some reason the following code seems to work:
q = '' for i in range(4,len(x)-4): if x[i].islower() and x[i-4].islower() and x[i-3:i].isupper() and x[i+1:i+4].isupper() and x[i+4].islower(): q+=x[i] print(q)
Advertisement
Answer
What you are trying to do is match a pattern : Not Upper, Upper, Upper, Upper, Not Upper, Upper, Upper, Upper, Not Upper. This is easier to catch if you use a signature for your string:
>>> t = "This is some TEXtTHAt will result in one true result" >>> sig = "".join("U" if c.isupper() else "l" for c in t) >>> sig 'UllllllllllllUUUlUUUllllllllllllllllllllllllllllllll'
You are looking for the lUUUlUUUl
substrings in the sig
string. Python has no builtin for findall
, but you can to iterate over the results of find
:
>>> result = "" >>> i = sig.find("lUUUlUUUl") >>> while i != -1: # find != -1 means that the substring wasn't found ... result += t[i+4] # the center `l` ... i = sig.find("lUUUlUUUl", i+4) # we can start the next search at the center `l` ... >>> print( result ) t
You may also use re.finditer
with a regex pattern, but is more complicated.