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

python - How to do a case-insensitive string.startswith - Stack Overflow
I need to use lowercase (a, e, i, o, u) and also uppercase (A, E, I, O, U) in self.all = variable.startswith(('a', 'e' , 'i', 'o', 'u')). I don't want two variables for Apple and for apple, but alw... More on stackoverflow.com
🌐 stackoverflow.com
help with IGNORECASE
https://docs.python.org/3/library/stdtypes.html#str.startswith startswith doesn't take an "ignore case" parameter because there are no circumstances under which it will ignore case. More on reddit.com
🌐 r/learnpython
2
1
October 31, 2022
String comparison in Python that is case-insensitive for first letter - Stack Overflow
I need to match the following string File system full. The problem is Starting F can be lowercase or capital. How can I do this in Python when string comparisons are usually case-sensitive? More on stackoverflow.com
🌐 stackoverflow.com
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
People also ask

Is Python startswith() case-sensitive?
Yes by default. "Python".startswith("py") returns False because uppercase and lowercase letters are compared exactly.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python startswith()
Python startswith(): Check String Prefix, Ignore Case, and Tuple ...
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 ...
🌐
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.
🌐
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...
🌐
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
Find elsewhere
🌐
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
Run the script using the python ... to perform case-insensitive prefix checks by converting both the string and the prefix to lowercase before using the startswith() method....
🌐
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() ... case-insensitive 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....
🌐
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 ...
🌐
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.
🌐
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
🌐
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 startswith string method has one mandatory argument which must be in a string format this argument is matched the start of the string object. Unlike strip string methods, this argument must match the order and is also case-sensitive.
🌐
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 - Le 17/02/2013 00:58, Biju a écrit ... to do a case insensitive search. > For example to filter items displayed in list on a page. > Also on other applications, say any word processor, or in page search > in Firefox, IE, Chrome etc. > > So can we make the default behavior of new methods String.startsWith, > ...
🌐
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 ...
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
April 22, 2025 - Explanation: This code uses casefold() for accurate case-insensitive comparison, especially with international characters. It converts all strings, checks uniqueness using a set and prints "equal" if all are identical otherwise, "unequal". re.match() checks if a string matches a pattern from the start and with the re.IGNORECASE flag, it ignores case differences.