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 Overflow
Top answer
1 of 7
70

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.

2 of 7
39

How about this:

prefix = 'he'
if myVeryLongStr[:len(prefix)].lower() == prefix.lower()
🌐
Reddit
reddit.com › r/learnpython › case insensitive using startswith
r/learnpython on Reddit: Case insensitive using startswith
May 13, 2020 -

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

Discussions

Case insensitive string comparisons. Know how to do it, really don't like it, deciding if it can be refined or if it's just typical developer being too opinionated.
I don’t have an answer to your question. I just want to add that you should use string.Equals(myString…) rather than myString.Equals() as that handles the case where myString is null. And yes, that makes it even more verbose 🙃 More on reddit.com
🌐 r/csharp
18
1
December 12, 2021
Case insensitive using startswith
maybe str.lower().str.startswith() would work? More on reddit.com
🌐 r/learnpython
6
1
May 13, 2020
People also ask

How do you make startswith() case-insensitive?
Normalize both strings the same way, for example text.casefold().startswith(prefix.casefold()). Convert the prefix too, not only the main string.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python startswith()
Python startswith(): Check String Prefix, Ignore Case, and Tuple ...
What does startswith() do in Python?
startswith() returns True if a string begins with the given prefix, otherwise False. It does not change the original string.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python startswith()
Python startswith(): Check String Prefix, Ignore Case, and Tuple ...
What is the difference between startswith() and in?
startswith() checks the beginning of the string. in checks whether a substring appears anywhere.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python startswith()
Python startswith(): Check String Prefix, Ignore Case, and Tuple ...
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python startswith()
Python startswith(): Check String Prefix, Ignore Case, and Tuple Examples
June 23, 2026 - Use lower() or casefold() on both the string and prefix for case-insensitive checks. Pass a tuple to check multiple prefixes. Use start and end when you need to test a specific range.
🌐
YouTube
youtube.com › hey delphi
PYTHON : Case-insensitive string startswith in Python - YouTube
PYTHON : Case-insensitive string startswith in PythonTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I'm going t...
Published: May 11, 2023
Views: 18
🌐
pythontutorials
pythontutorials.net › blog › case-insensitive-string-startswith-in-python
Efficient Case-Insensitive String Startswith in Python: Optimizing for Long Strings Without Lower() — pythontutorials.net
In Python, checking if a string ... text). The naive approach—converting both the main string and prefix to lowercase (or uppercase) with `lower()` (or `upper()`) and then using `startswith()`—works for small stri...
🌐
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › str.startswith
Case Sensitivity & Tuples: Avoiding Common Mistakes with Python's startswith()
my_string = "Apple Pie" prefix = "apple" # Trouble: False because of case print(f"Case-sensitive check: {my_string.startswith(prefix)}") # Output: Case-sensitive check: False # Solution: Convert both to lowercase print(f"Case-insensitive check: {my_string.lower().startswith(prefix.lower())}") # Output: Case-insensitive check: True
Find elsewhere
🌐
DNMTechs
dnmtechs.com › implementing-case-insensitive-string-startswith-in-python-3
Implementing Case-Insensitive String Startswith in Python 3 – DNMTechs – Sharing and Storing Technology Knowledge
In the above example, the startswith() ... startswith(), we can leverage Python’s built-in string methods and convert both the string and the prefix to lowercase or uppercase before performing the check....
🌐
Codecademy
codecademy.com › docs › python › strings › .startswith()
Python | Strings | .startswith() | Codecademy
April 17, 2025 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. ... The .startswith() method returns True if the input string starts with the given value and False if it happens otherwise.
🌐
YouTube
youtube.com › watch
How to Perform a case-insensitive Check for String Starts With in Python - YouTube
Discover how to effectively handle case-insensitive string checks in Python using `startswith`. Learn two simple methods with examples.---This video is based...
Published: April 16, 2025
Views: 2
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-a-string-starts-with-a-prefix-in-python-559583
How to Check If a String Starts with a Prefix in Python | LabEx
You can also use the upper() method to convert both strings to uppercase for a case-insensitive comparison. The key is to ensure that both strings are in the same case before using startswith(). In this lab, you learned about string prefixes ...
🌐
sqlpey
sqlpey.com › python › top-5-methods-to-case-insensitive-string-startswith-in-python
Top 5 Methods to Perform Case-Insensitive String StartsWith in Python
November 24, 2024 - The typical way to check if a string begins with a specific prefix, case insensitively, is by using the lower() method: # Standard Approach mystring = "Hello, World!" result = mystring.lower().startswith("he") print(result) # Output will be True · While this works perfectly well, it can become ...
🌐
CSDN
devpress.csdn.net › python › 63045029c67703293080add1.html
Case-insensitive string startswith in Python - DevPress官方社区
August 23, 2022 - 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
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › startswith()
Python str startswith() - Check Prefix Presence
December 11, 2024 - The startswith() method in Python is a string method that checks whether a given string starts with a specified prefix. This check is case-sensitive and plays a critical role in data validation, parsing, and filtering tasks when dealing with ...
🌐
datagy
datagy.io › home › python posts › python strings › python string startswith: check if string starts with substring
Python String startswith: Check if String Starts With Substring • datagy
December 16, 2022 - Then, we can use the .startswith() method to check if a string starts with a pattern using case insensitivity. In this section, you’ll learn how to use the Python .startswith() method to check if items in a list of strings start with a substring.
🌐
Blogger
learnpythontutorial.blogspot.com › 2015 › 10 › python-tutorial-python-startswith.html
Learn Python: Python Tutorial: Python Startswith String Method - Python Strings #71
October 6, 2015 - The argument is case-sensitive and the order must also match. start - The start argument will indicate where the startswith string method will start the search for an argument match.
🌐
AskPython
askpython.com › python › string › python-string-startswith-function
Python String startswith() Function - AskPython
August 6, 2022 - String in Python has built-in functions for almost every action to be performed on a string. Python String startswith() function checks for the specific prefix in a string and returns True else False.
🌐
Esdiscuss
esdiscuss.org › topic › case-insensitive-string-startswith-contains-endswith-replaceall-method
Case insensitive String startsWith, contains, endsWith, replaceAll method
February 18, 2013 - And to make it case sensitive we should add a third flag parameter matchCase like... var startsWith = str.startsWith(searchString [, position [, matchCase] ] ); var contained = str.contains(searchString [, position [, matchCase] ] ); var endsWith = str.endsWith(searchString [, position [, ...
🌐
ExceptionsHub
exceptionshub.com › case-insensitive-string-startswith-in-python.html
Case-insensitive string startswith in Python | ExceptionsHub
January 2, 2018 - 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
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › startswith › python-string-startswith
Python startswith() function | Why do we use Python String startswith()? |
September 27, 2021 - The Python startswith() string ... Python startwith() function follows the below-mentioned syntax: ... Note – This function is case-sensitive....