Knowee
Questions
Features
Study Tools

Write a program to replace all spaces from text with - (dash)from the file podar.txt.

Question

Write a program to replace all spaces from text with - (dash)from the file podar.txt.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python program that reads a file, replaces all spaces with dashes, and writes the result back to the file.

# Open the file in read mode and read its content
with open('podar.txt', 'r') as file:
    content = file.read()

# Replace all spaces with dashes
content = content.replace(' ', '-')

# Open the file in write mode and write the modified content
with open('podar.txt', 'w') as file:
    file.write(content)

This program works as follows:

  1. It opens the file podar.txt in read mode and reads its content into the variable content.
  2. It uses the replace method of the string class to replace all spaces in content with dashes. The result is stored back in content.
  3. It opens the file podar.txt again, this time in write mode, and writes the modified content back to the file. This overwrites the original content of the file.

Please replace 'podar.txt' with your actual file path and name.

This problem has been solved

Similar Questions

Create a text file "podar.txt" in python and ask the user towrite a single line of text by user input

ect answerWhich of the following formatting options can be used in order to add ‘n’ blank spaces after a given string ‘S’?Optionsprint(“-ns”%%S)print(“%ns”%S)print(“-ns”%S)print(“%-ns”%S)

Write a Python program to replace whitespaces with an underscore and vice versa.

Ninja wants to print a book of stories. He created a doc file and sent it to his editor to make some edits. But the file got corrupted due to some reasons and made changes in the original file. Ninja did not have a duplicate file of the same, so he wants to correct the same file. He found that the file has been changed in such a way that all the spaces have been removed from the file and the first letter after each space that used to be has been changed to the equivalent uppercase characters.Example:If the corrupted file looks like “CodingNinjasIsACodingPlatform”, then the original file was: “coding ninjas is a coding platform”.Ninja needs to change the corrupted file to the original file.Note:You need to convert all the uppercase characters to lowercase characters, and you need to add a single space between every two words.

Which of the following formatting options can be used in order to add ‘n’ blank spaces after a given string ‘S’?Optionsprint(“-ns”%S)print(“-ns”%%S)print(“%ns”%S)print(“%-ns”%S)

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.