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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:04:00+00:00 2026-06-14T06:04:00+00:00

I need to replace all words in a text document that are of length

  • 0

I need to replace all words in a text document that are of length 4 with a different word.

For example, if a text document contained the phrase “I like to eat very hot soup” the words “like”, “very”, and “soup” would be replaced with “something”

Then, instead of overwriting the original text document, it needs to create a new one with the changed phrase.

Here is what I have so far:

def replacement():  
    o = open("file.txt","a") #file.txt will be the file containing the changed phrase
    for line in open("y.txt"):  #y.txt is the original file
        line = line.replace("????","something")  #see below
        o.write(line + "\n")
    o.close()

I’ve tried changing “????” to something like

(str(len(line) == 4)

but that didn’t work

  • 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-14T06:04:01+00:00Added an answer on June 14, 2026 at 6:04 am

    First lets make a function that returns something if it’s given a word of length 4 and the word it was given otherwise:

    def maybe_replace(word, length=4):
      if len(word) == length:
        return 'something'
      else:
        return word
    

    Now lets walk through your for loop. In each iteration you have a line of your original file. Lets split that into words. Python gives us the split function that we can use:

       split_line = line.split()
    

    The default is to split on whitespace, which is exactly what we want. There’s more documentation if you want it.

    Now we want to get the list of calling our maybe_replace function on every word:

      new_split_line = [maybe_replace(word) for word in split_line]
    

    Now we can join these back up together using the join method:

      new_line = ' '.join(new_split_line)
    

    And write it back to our file:

      o.write(new_line + '\n')
    

    So our final function will be:

    def replacement():  
      o = open("file.txt","a") #file.txt will be the file containing the changed phrase
      for line in open("y.txt"):  #y.txt is the original file
        split_line = line.split()
        new_split_line = [maybe_replace(word) for word in split_line]
        new_line = ' '.join(new_split_line)
        o.write(new_line + '\n')
      o.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace a text using javascript and regex. Such that the words
I need to replace all words starting with @ in a text with the
I have a document containing many quotation marks. I need to replace all pairs
Need to replace a domain name on all the links on the page that
I need to replace all & symbols with &#38 in my text file but
I need to be able to replace all occurrences of the word and ONLY
So I figured out that the replace function, that finds all single-letter words (prepositions
I need to replace all System.Environment.Newline(s) in the string returned by my function with
I have a search box on my site and i need to replace all
I have combined MathJax and Markdown markup and therefore I need to replace all

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.