Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:59:51+00:00 2026-06-14T22:59:51+00:00

Here is how I check whether mystring begins with some string: >>> mystring.lower().startswith(he) True

  • 0

Here is how I check whether mystring begins with some string:

>>> mystring.lower().startswith("he")
True

The problem is that mystring is very long (thousands of characters), so the lower() operation takes a lot of time.

QUESTION: Is there a more efficient way?

My unsuccessful attempt:

>>> import re;
>>> mystring.startswith("he", re.I)
False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:59:53+00:00Added an answer on June 14, 2026 at 10:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text in a string, I need to check whether that particular
How can I check whether a string represents a long, a double, or just
i want to check whether string present .png , .pdf , .html for that
I took some code here: Check if role consists of particular user in DB?
Can check out here alt text http://51hired.com/static/problem.bmp
I already check some of asp.net mvc hosting sites listed here: https://stackoverflow.com/questions/637567/affordable-stable-asp-net-mvc-hosting-exist I worry
I want to check whether string contains particular sub string and using CONTAINS() for
I basically need a function to check whether a string's characters (each character) is
I want to parse the strings, so that to check whether they have specified
I wrote this sample code to check whether integer or string index is better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.