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

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
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
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

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 ...
What is the difference between startswith() and removeprefix()?
startswith() returns True or False. removeprefix() returns a new string with the prefix removed when it is present.
🌐
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.
🌐
Codecademy
codecademy.com › docs › python › strings › .startswith()
Python | Strings | .startswith() | Codecademy
April 17, 2025 - The following codebyte example ... whereas .endswith() checks if a given string ends with a specific value. Yes, .startswith() is case-sensitive....
🌐
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
To perform a case-insensitive prefix check, you can convert both the string and the prefix to lowercase (or uppercase) before using the startswith() method.
🌐
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
Find elsewhere
🌐
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 starts with a specific prefix is a common operation, often required to be case-insensitive (e.g., validating user input, parsing log files, or filtering text).
🌐
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › str.startswith
Case Sensitivity & Tuples: Avoiding Common Mistakes with Python's startswith()
This is the most frequent issue. str.startswith() performs a case-sensitive comparison. If you need a case-insensitive check, convert both the string and the prefix to the same case (usually lowercase) before comparison.
🌐
CSDN
devpress.csdn.net › python › 63045029c67703293080add1.html
Case-insensitive string startswith in Python - DevPress官方社区
August 23, 2022 - 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 ...
🌐
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 ...
🌐
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
To implement 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. By doing so, we ensure that the comparison is not affected by the case of the characters.
🌐
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.
🌐
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 - In the code block above, we first use the .lower() method to represent the string in lowercase. 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.
🌐
Esdiscuss
esdiscuss.org › topic › case-insensitive-string-startswith-contains-endswith-replaceall-method
Case insensitive String startsWith, contains, endsWith, replaceAll method
February 18, 2013 - And sometimes, case-sensitive is what you want. I agree, that is why I mentioned to add matchCase parameter. or have startsWithI, containsI, endsWithI instead
🌐
ExceptionsHub
exceptionshub.com › case-insensitive-string-startswith-in-python.html
Case-insensitive string startswith in Python | ExceptionsHub
January 2, 2018 - 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