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 7500155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:04:58+00:00 2026-05-29T20:04:58+00:00

So far I’ve come up with the method below but my question is is

  • 0

So far I’ve come up with the method below but my question is is there a shorter method out there that has the same result?

My Code :

input_str       = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
replace_str     = "stuff"
replacer_str    = "banana"


print input_str
# prints: myStrIngFullOfStUfFiWannAReplaCE_StUfFs

if replace_str.lower() in input_str.lower():            # Check if even in the string
    begin_index = input_str.lower().find( replace_str )
    end_index   = begin_index + len( replace_str )

    replace_section = input_str[ begin_index : end_index ]

    case_list = []
    for char in replace_section:                        # Get cases of characters in the section to be replaced
        case_list.append( char.istitle() )

    while len( replacer_str ) > len(case_list):
        case_list += case_list

    sameCase_replacer_str   = ""                        # Set match the replacer string's case to the replace
    replacer_str            = replacer_str.lower()
    for index in range( len(replacer_str) ):
        char = replacer_str[ index ]
        case = case_list[ index ]
        if case == True:
            char = char.title()

        sameCase_replacer_str += char

    input_str = input_str.replace( replace_section , sameCase_replacer_str )

print input_str
# prints: myStrIngFullOfBaNaNAiWannAReplaCE_BaNaNAs
  • 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-05-29T20:05:00+00:00Added an answer on May 29, 2026 at 8:05 pm

    I’d use something like this:

    import re
    
    def replacement_func(match, repl_pattern):
        match_str = match.group(0)
        repl = ''.join([r_char if m_char.islower() else r_char.upper()
                       for r_char, m_char in zip(repl_pattern, match_str)])
        repl += repl_pattern[len(match_str):]
        return repl
    
    input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs"
    print re.sub('stuff',
                 lambda m: replacement_func(m, 'banana'),
                 input_str, flags=re.I)
    

    Example output:

    myStrIngFullOfBaNaNaiWannAReplaCE_BaNaNas

    Notes:

    • This handles the case in which the different matches have different upper/lower case combinations.
    • It’s assumed that the replacement pattern is in lower case (that’s very easy to change, anyway).
    • If the replacement pattern is longer than the match, the same case as in the pattern is used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I've got: :notice :alert :error but is there definitive list, that can
So far my code stops the sound but once it has stopped the sound
So far i have the following code, but it doesn't seem to be working,
So far I've got the following regex that matches against regular domains but not
As far as I know, Flash has to pass info off to another external
as far as I can get gowalla auth code response contains both expires_at and
As far as I remember, in pure W3C event model (that means, using addEventListener),
As far as I know there's Erlang plugin for NetBeans and Eclipse. Which one
Far I've found that giving the iframe a background colour will alter the timeline
As far as I know (which is very little) , there are two ways,

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.