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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:08:07+00:00 2026-06-11T09:08:07+00:00

String replacement in Python is not difficult, but I want to do something special:

  • 0

String replacement in Python is not difficult, but I want to do something special:

teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']
#replace 'test' with random item from animals
finalstr = ['dog fox dog monkey']

I write a very inefficient version:

from random import choice
import string
import re

teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']

indexes = [m.start() for m in re.finditer('test', 'test test test test')]
#indexes = [0, 5, 10, 15]
for i in indexes:
    string.replace(teststr, 'test', choice(animals), 1)

#Final result is four random animals
#maybe ['dog fox dog monkey']

It works, but I believe there is some simple method with REGULAR EXPRESSION which I am not familiar with.

  • 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-11T09:08:08+00:00Added an answer on June 11, 2026 at 9:08 am

    Use a re.sub callback:

    import re
    import random
    
    animals = ['bird','monkey','dog','fox']
    
    def callback(matchobj):
        return random.choice(animals)
    
    teststr = 'test test test test'
    ret = re.sub(r'test', callback, teststr)
    print(ret)
    

    yields (for example)

    bird bird dog monkey
    

    The second argument to re.sub can be a string or a function (i.e. a callback). If it is a function, it is called for every non-overlapping occurrance of the regex pattern, and its return value is substituted in place of the matched string.

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

Sidebar

Related Questions

What's the easiest way to do a case-insensitive string replacement in Python?
I'm newbie in Python, but this question is not a homework (actually this code
I want to pass a function call(which returns a string) as a replacement string
I know how to make string replacements in Python but I need a way
Python 2.6 Using Python string.replace() seems not working for UTF-16-LE file. I think of
I am struggling with string replacement that contains special regex characters. I got a
I want to be able to do the following string replacement: Input: 3^4 Output:
I need some help with the Python's new string formatter. Basically, I want the
Python has re.escape() if you want to match an arbitrary string literal. In my
Possible Duplicate: How do you perform string replacement on just a subsection of a

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.