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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:01:44+00:00 2026-06-03T07:01:44+00:00

When using re.sub() part of re for python, a function can be used for

  • 0

When using re.sub() part of re for python, a function can be used for sub if I am not mistaken. To my knowledge it passes in the match to whatever function is passed for example:

r = re.compile(r'([A-Za-z]')
r.sub(function,string)

Is there a smarter way to have it pass in a second arg other than with a lambda that calls a method?

r.sub(lambda x: function(x,arg),string)
  • 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-03T07:01:46+00:00Added an answer on June 3, 2026 at 7:01 am

    You can use functools.partial:

    >>> from functools import partial
    >>> def foo(x, y):
    ...     print x+y
    ... 
    >>> partial(foo, y=3)
    <functools.partial object at 0xb7209f54>
    >>> f = partial(foo, y=3)
    >>> f(2)
    5
    

    In your example:

    def function(x, y):
         pass # ...
    r.sub(functools.partial(function, y=arg),string)
    

    And a real use with regexes:

    >>> s = "the quick brown fox jumps over the lazy dog"
    >>> def capitalize_long(match, length):
    ...     word = match.group(0)
    ...     return word.capitalize() if len(word) > length else word
    ... 
    >>> r = re.compile('\w+')
    >>> r.sub(partial(capitalize_long, length=3), s)
    'the Quick Brown fox Jumps Over the Lazy dog'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not able to replace the string (-) using re.sub in Python. >>>
Using Crystal Report 7 Single report (not sub report added, group by id) ID
hello i'm using vb 2008 here is part of my code: Private Declare Function
Using File::Find , how can I pass parameters to the function that processes each
I am trying to achieve the following without using sub query. For a funding,
We are currently using Fitnesse for sub system testing. [All our applications are in
I want to create sub-domains using PHP on the fly. Suppose a user registers
In Excel I have attached a picture to a cell using the following Sub
Using the following code: Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer
using crystal report 7 Single report (no sub report added, group by id) ID

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.