0

In python to check if 'apple' appears in another string we do:

if 'apple' in my_str:

To check without cases sensitive I read we can do:

if 'apple' in my_str.lower():

But what if my_str is REALLY long, it's not efficent to call .lower() on it... Doesn't python support native non cases sensitive match?

Mark
  • 90,562
  • 7
  • 108
  • 148
David
  • 9
  • 7
  • 1
    why isn't it efficient? – Sayse Jan 14 '22 at 21:19
  • 4
    You can use the regex case insensitivity flag. https://stackoverflow.com/questions/9655164/regex-ignore-case-sensitivity – kpie Jan 14 '22 at 21:22
  • @Sayse Consider something extreme like `my_str = "apple" + "x" * 1000000`. There's no need to construct a new megastring (literally) just to find out the first 5 characters of the original match. – chepner Jan 14 '22 at 21:33
  • @chepner - oh yea I get that, I was trying to get the op to explain their issue more… I.e where does the string come from (would you use a file stream and find the first character perhaps). What does the string look like or can the data be modified before. Would “pineapple” be acceptable etc. the accepted answer using findall still traverses the whole string rather than short circuiting and there’s almost certainly duplicates to this and didn’t want to just apply the first one I saw – Sayse Jan 15 '22 at 09:21

1 Answers1

2

You can use regular expressions and re.IGNORECASE In python Case insensitive regular expression without re.compile?

import re
s = 'padding aAaA padding'
print(bool(re.findall("aaaa",s,re.IGNORECASE)))

Prints True.

kpie
  • 9,588
  • 5
  • 28
  • 50