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?

4
  • 1
    why isn't it efficient? Commented Jan 14, 2022 at 21:19
  • 4
    You can use the regex case insensitivity flag. stackoverflow.com/questions/9655164/… Commented Jan 14, 2022 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. Commented Jan 14, 2022 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 Commented Jan 15, 2022 at 9:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.