Simply call lower to make the string lowercase before calling endswith:

ext = (".dae", ".xml", ".blend", ".bvh", ".3ds", ".ase",
           ".obj", ".ply", ".dxf", ".ifc", ".nff", ".smd",
           ".vta", ".mdl", ".md2", ".md3"
           ".pk3", ".mdc", ".x"
           ".q3o", ".q3s", ".raw"
           ".ac", ".dxf", ".irrmesh"
           ".irr", ".off", ".ter"
           ".mdl", ".hmp", ".mesh.xml"
           ".skeleton.xml", ".material", ".ms3dv"
           ".lwo", ".lws", ".lxo"
           ".csm", ".cob", ".scn"
           ".xgl", ".zgl")
for folder, subfolders, filename in os.walk(directory):
    if any([filename.lower().endswith(tuple(ext)) for filename in filenames]):
Answer from javidcf on Stack Overflow
Top answer
1 of 2
55

Simply call lower to make the string lowercase before calling endswith:

ext = (".dae", ".xml", ".blend", ".bvh", ".3ds", ".ase",
           ".obj", ".ply", ".dxf", ".ifc", ".nff", ".smd",
           ".vta", ".mdl", ".md2", ".md3"
           ".pk3", ".mdc", ".x"
           ".q3o", ".q3s", ".raw"
           ".ac", ".dxf", ".irrmesh"
           ".irr", ".off", ".ter"
           ".mdl", ".hmp", ".mesh.xml"
           ".skeleton.xml", ".material", ".ms3dv"
           ".lwo", ".lws", ".lxo"
           ".csm", ".cob", ".scn"
           ".xgl", ".zgl")
for folder, subfolders, filename in os.walk(directory):
    if any([filename.lower().endswith(tuple(ext)) for filename in filenames]):
2 of 2
2

This is a really old answer but since Python 3.3, there exists the casefold() method which is more aggressive than lower() and is the more natural caseless string matching. So something like the following is an option:

filename.casefold().endswith(ext)

On a tangential note, any() short-circuits, meaning it stops at the first True, so it is much faster if any() is called on a generator expression instead of a list (especially if the matching string is towards the beginning of a long list) because with genexpr, we can stop the endswith check right away while with list, we have to perform the endswith check for every string before the any() evaluation.

So instead of

any([filename.lower().endswith(ext) for filename in filenames])
#   ^                                                        ^  <--- list

use

any(filename.lower().endswith(ext) for filename in filenames)
#  ^                                                        ^   <--- genexpr

Finally, since this question is tagged regex, here's a regex solution as well. Simply compile a pattern that ignores case and search whether the pattern matches.

import re
pat = re.compile(fr"({'|'.join(re.escape(e) for e in ext)})$", re.I)
for folder, subfolders, filenames in os.walk('.'):
    if any(pat.search(filename) for filename in filenames):
        # do something
🌐
Codecademy
codecademy.com › docs › python › strings › .endswith()
Python | Strings | .endswith() | Codecademy
April 21, 2025 - Yes. Any string ends with an empty string, so calling .endswith("") on any string will return True. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
Dive into Python
diveintopython.org › home › functions & methods › string methods › endswith()
endswith() in Python - String Methods with Examples
The endswith() method is case sensitive by default, but you can use case-insensitive comparison by converting the string to lowercase. text = 'Python Programming' print(text.lower().endswith('programming')) capitalize() casefold() center() count() encode() expandtabs() find() format() format_map() ...
🌐
Scaler
scaler.com › home › topics › endswith() python
endswith() python | endswith() Function in Python - Scaler Topics
March 30, 2022 - If we have passed a tuple of strings, then if any of the elements of the tuple matches the string, then also the endswith() will return True. Note: Case-sensitive means if the suffix content is in lower-case, and the string content is upper-case, then the method endswith() will also return False.
🌐
datagy
datagy.io › home › python posts › python strings › python string endswith: check if string ends with substring
Python String endswith: Check if String Ends With Substring • datagy
July 25, 2023 - In the code block above, we first use the .lower() method to represent the string in lowercase. Then, we can use the .endswith() method to check if a string ends with a pattern using case insensitivity. In this final section, you’ll learn how to use the Python endswith() method to check whether ...
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-endswith-8569d0441acc
Understanding pandas endswith. The biggest lie in data science? That… | by Hey Amit | Medium
April 12, 2025 - Is pandas endswith case-sensitive? Yes, the endswith method is case-sensitive. If you need a case-insensitive check, consider normalizing the string data using .str.lower() before using endswith.
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › endswith › python-string-endswith
Python endswith() function | Why do we use Python String endswith()? |
September 27, 2021 - Let us look at few basic examples to understand the working of Python endswith(). ... # Python program to illustrate endswith() text = 'Jerry loves to eat cheese' print('Original string:', text) result = text.endswith('cheese') print('String ends with - cheese? :', result) result = text.endswith('eat') print('String ends with - eat? :', result) # case-sensitive result = text.endswith('Cheese') print('String ends with - Cheese?
🌐
Data Science Parichay
datascienceparichay.com › home › blog › python string endswith – with examples
Python String Endswith - With Examples - Data Science Parichay
October 4, 2020 - # check if string ends with the suffix s = "To code or not to code" # check if s ends with 'to code' print("Ends with 'to code':", s.endswith('to code')) # check if the match is case-sensitive or not print("Ends with 'TO CODE':", s.endswith('TO ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
April 22, 2025 - For example, if the input is "gfg is BeSt", then the out ... Sometimes, while working with Python strings, we might have a problem in which we have list of strings and we wish to convert them into specified cases.
🌐
Esdiscuss
esdiscuss.org › topic › case-insensitive-string-startswith-contains-endswith-replaceall-method
Case insensitive String startsWith, contains, endsWith, replaceAll method
February 18, 2013 - Can we have another set methods ... want to search something in a text, he/she wants to do a case insensitive search. For example to filter items displayed in list on a page....
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()
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-a-string-ends-with-a-suffix-in-python-559571
How to Check If a String Ends with a Suffix in Python | LabEx
This demonstrates that the endswith() method is case-sensitive. To perform a case-insensitive check, you can convert the string to lowercase using the lower() method before using endswith().
🌐
AskPython
askpython.com › python › string › python-string-endswith-function
Python String endswith() function - AskPython
August 6, 2022 - ... Return Type: Boolean i.e. True ... Suffix, Start, End ... str = 'Engineering is an interesting discipline' print(str.endswith('discipline', 2)) # True print(str.endswith('Engineering', 10)) # False · Example 3: Using the len() ...
🌐
LearnPython.com
learnpython.com › blog › python-case-sensitive
Is Python Case-Sensitive? | LearnPython.com
This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings. The example code shows how the lower() method works.
🌐
W3Schools
w3schools.com › python › ref_string_endswith.asp
Python String endswith() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The endswith() method returns True if the string ends with the specified value, otherwise False.
🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
May 21, 2024 - If you want to perform case-insensitive string comparison, you can convert the strings to lowercase or uppercase using the lower() or upper() string methods before performing the comparison. You can also check if strings are equivalent using unicodedata: # -*- coding: utf-8 -*- # String comparison using unicode in Python # Example strings with unicode characters string1 = "Café" string2 = "Cafe\u0301" # Method 1: Using the unicode normalization method import unicodedata # Normalize strings using NFKC normalization form normalized_string1 = unicodedata.normalize("NFKC", string1) normalized_str
🌐
TutorialsPoint
tutorialspoint.com › article › How-do-I-do-a-case-insensitive-string-comparison-in-Python
How do I do a case-insensitive string comparison in Python?
June 10, 2025 - The casefold() method in Python is similar to the lower() method, but the difference is caseless matching, which is useful for comparing strings containing special characters or Unicode. This is recommended when working with Unicode data. str1 = "Straße" str2 = "strasse" if str1.casefold() == str2.casefold(): print("The strings are equal (case-insensitive with casefold)") else: print("The strings are not equal")
🌐
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.