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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:29:47+00:00 2026-05-30T16:29:47+00:00

I am writing a program like unix tr, which can replace string of input.

  • 0

I am writing a program like unix tr, which can replace string of input.
My strategy is to find all indexes of source strings at first, and then replace them by target strings. I don’t know how to replace string by index, so I just use the slice.
However, if the length of target string doesn’t equal to the source string, this program will be wrong. I want to know what’s the way to replace string by index.

def tr(srcstr,dststr,string):
    indexes = list(find_all(string,srcstr)) #find all src indexes as list
    for index in indexes:
        string = string[:index]+dststr+string[index+len(srcstr):]
    print string

tr(‘aa’,’mm’,’aabbccaa’)
the result of this will be right: mmbbccmm
but if tr(‘aa’,’ooo’,’aabbccaa’), the output will be wrong:

ooobbcoooa

  • 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-30T16:29:48+00:00Added an answer on May 30, 2026 at 4:29 pm

    Python strings are immutable (as far as I can remember), so you can’t just go in and insert stuff.

    Luckily Python already has a replace() function.

    >>> s = "hello, world!"
    >>> s1 = s.replace("l", "k")
    >>> print s1
    hekko, workd!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can one loop a command/program in a Unix shell without writing the loop
i'm writing a program which do something like that: - (void)viewDidLoad { [super viewDidLoad];
I'm currently writing a program in which I would like to access the variable
So I'm writing a program which keeps a track of various documents like e-mails,
I am writing a java program that runs under unix. It would like run
I am writing a program in assembly and it isn't working, so I'd like
I am writing a C program in Linux. Commands like execv() require a path
I'd like to add video conversion capabilities to a program I'm writing. FFmpeg's command
I woud like to create a cross-platform drawing program. The one requirement for writing
I am writing my first serious wxWidgets program. I'd like to use the wxConfig

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.