You could use a regular expression as follows:
In [33]: bool(re.match('he', 'Hello', re.I))
Out[33]: True
In [34]: bool(re.match('el', 'Hello', re.I))
Out[34]: False
On a 2000-character string this is about 20x times faster than lower():
In [38]: s = 'A' * 2000
In [39]: %timeit s.lower().startswith('he')
10000 loops, best of 3: 41.3 us per loop
In [40]: %timeit bool(re.match('el', s, re.I))
100000 loops, best of 3: 2.06 us per loop
If you are matching the same prefix repeatedly, pre-compiling the regex can make a large difference:
In [41]: p = re.compile('he', re.I)
In [42]: %timeit p.match(s)
1000000 loops, best of 3: 351 ns per loop
For short prefixes, slicing the prefix out of the string before converting it to lowercase could be even faster:
In [43]: %timeit s[:2].lower() == 'he'
1000000 loops, best of 3: 287 ns per loop
Relative timings of these approaches will of course depend on the length of the prefix. On my machine the breakeven point seems to be about six characters, which is when the pre-compiled regex becomes the fastest method.
In my experiments, checking every character separately could be even faster:
In [44]: %timeit (s[0] == 'h' or s[0] == 'H') and (s[1] == 'e' or s[1] == 'E')
1000000 loops, best of 3: 189 ns per loop
However, this method only works for prefixes that are known when you're writing the code, and doesn't lend itself to longer prefixes.
Answer from NPE on Stack OverflowYou could use a regular expression as follows:
In [33]: bool(re.match('he', 'Hello', re.I))
Out[33]: True
In [34]: bool(re.match('el', 'Hello', re.I))
Out[34]: False
On a 2000-character string this is about 20x times faster than lower():
In [38]: s = 'A' * 2000
In [39]: %timeit s.lower().startswith('he')
10000 loops, best of 3: 41.3 us per loop
In [40]: %timeit bool(re.match('el', s, re.I))
100000 loops, best of 3: 2.06 us per loop
If you are matching the same prefix repeatedly, pre-compiling the regex can make a large difference:
In [41]: p = re.compile('he', re.I)
In [42]: %timeit p.match(s)
1000000 loops, best of 3: 351 ns per loop
For short prefixes, slicing the prefix out of the string before converting it to lowercase could be even faster:
In [43]: %timeit s[:2].lower() == 'he'
1000000 loops, best of 3: 287 ns per loop
Relative timings of these approaches will of course depend on the length of the prefix. On my machine the breakeven point seems to be about six characters, which is when the pre-compiled regex becomes the fastest method.
In my experiments, checking every character separately could be even faster:
In [44]: %timeit (s[0] == 'h' or s[0] == 'H') and (s[1] == 'e' or s[1] == 'E')
1000000 loops, best of 3: 189 ns per loop
However, this method only works for prefixes that are known when you're writing the code, and doesn't lend itself to longer prefixes.
How about this:
prefix = 'he'
if myVeryLongStr[:len(prefix)].lower() == prefix.lower()
I'm trying to do a quick database search in python using pandas.
data = pd.read_excel(open('Stockroom_Inventory_May_4_2020.xlsx', 'rb'),sheet_name='Chemical')
#This works fine for case insensitive search
data.loc[data['Item Name *'].str.contains('Ferro', case = False)][['Item Name *','Location','Sub-location','Location Details']]
#Can't use case = False with startswith
data.loc[data['Item Name *'].str.startswith('Ferro')][['Item Name *','Location','Sub-location','Location Details']]
Is there a way to get around this, even making everything from the excel file lowercase would be fine.
Thanks for your help
What does startswith() do in Python?
What is the difference between startswith() and in?
What is the difference between startswith() and removeprefix()?
I have simple find loop but I want it to ignore case and I just cant get it to .
import re
names = ['Tilt back', 'speed', 'gist']
for name in names:
if name.startswith('tilt', re.IGNORECASE):
print(name)You could convert the string to lowercase before checking.
variable.lower().startswith(('a', 'e', 'i', 'o', 'u'))
Alternatively, you could use a regular expression with the ignore case flag.
bool(re.match('(?i)[aeiou]', variable))
you can use the .upper() command or .lower() command to convert all characters in the string to upper or lowercase
class Vocal:
def __init__(self, variable):
self.Vocal = variable.lower().startswith(('a', 'e', 'i', 'o', 'u'))