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

  • Home
  • SEARCH
  • 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 9275039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:29:43+00:00 2026-06-18T16:29:43+00:00

So I have a string ‘1xxx1’ and I want to replace a certain number

  • 0

So I have a string ‘1xxx1’ and I want to replace a certain number (maybe all maybe none) of x’s with a character, let’s say ‘5’. I want all possible combinations (…maybe permutations) of the string where x is either substituted or left as x. I would like those results stored in a list.

So the desired result would be

>>> myList = GenerateCombinations('1xxx1', '5')
>>> print myList
['1xxx1','15xx1','155x1','15551','1x5x1','1x551','1xx51']

Obviously I’d like it to be able to handle strings of any length with any amount of x’s as well as being able to substitute any number. I’ve tried using loops and recursion to figure this out to no avail. Any help would be appreciated.

  • 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-18T16:29:44+00:00Added an answer on June 18, 2026 at 4:29 pm

    How about:

    from itertools import product
    
    def filler(word, from_char, to_char):
        options = [(c,) if c != from_char else (from_char, to_char) for c in word]
        return (''.join(o) for o in product(*options))
    

    which gives

    >>> filler("1xxx1", "x", "5")
    <generator object <genexpr> at 0x8fa798c>
    >>> list(filler("1xxx1", "x", "5"))
    ['1xxx1', '1xx51', '1x5x1', '1x551', '15xx1', '15x51', '155x1', '15551']
    

    (Note that you seem to be missing 15x51.)
    Basically, first we make a list of every possible target for each letter in the source word:

    >>> word = '1xxx1'
    >>> from_char = 'x'
    >>> to_char = '5'
    >>> [(c,) if c != from_char else (from_char, to_char) for c in word]
    [('1',), ('x', '5'), ('x', '5'), ('x', '5'), ('1',)]
    

    And then we use itertools.product to get the Cartesian product of these possibilities and join the results together.

    For bonus points, modify to accept a dictionary of replacements. :^)

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

Sidebar

Related Questions

I have string asdf-zxcv-qwer- and I want to replace all occurence of words between
i have String from php, let say stringName={instruction:[1,2,3]} and i want to turn tat
I have String url = http://@50stackoverflow.com/questions/@50724043/@50xyz.html I want to replace @50 to space without
I have string like [any characters]{$\prime$}[any characters]$[any characters]{$\prime$}[any characters]$[any characters] I want to replace
I have string that displays UTF-8 encoded characters, and I want to convert it
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
Have String str May 23 2011 12:20:00, want to convert it to date such
i have string like this $string = '$foo$wow$123$$$ok$'; i want to return empty string
I have string arraylist [rose,flower,banana,parrot,rose,bird,flower,rose,banana,bird] From this array I want last index of each

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.