Python Replacing multiple spaces in Python

  • Thread starter Thread starter tabc
  • Start date Start date
  • Tags Tags
    Multiple Python
AI Thread Summary
The discussion revolves around creating a function, spacereplace(txt), that effectively replaces two or more consecutive spaces in a string with a single space while leaving all other characters unchanged. Initial attempts included using the replace method, which only addressed two spaces at a time, and a split-join method that was not accepted. A proposed solution involves iterating through each character in the string, checking for consecutive spaces, and only adding a single space to the new string if the previous character was also a space. This approach successfully handles multiple consecutive spaces by ensuring that only one space is retained in the output.
tabc
Messages
5
Reaction score
0

Homework Statement



Write a function spacereplace(txt) that removes two or more spaces in a row, replacing them with a single space. Any other characters should remain the same.

The Attempt at a Solution



I tried this:

def spacereplace(txt):
f=txt.replace(" "," ")
return f

This obviously just works when there are two spaces. I have no idea how to make it work for 2+.

This method apparently works:

def spacereplace(txt):
txt.split()
f=" ".join(txt.split())
return f

But it is not beeing accepted. Any ideas?
 
Technology news on Phys.org


Hello!

Here is one possible solution to your problem:

def spacereplace(txt):
new_txt = ""
prev_char = ""
for char in txt:
if char == " " and prev_char == " ":
continue
else:
new_txt += char
prev_char = char
return new_txt

Explanation: This function loops through each character in the input string and checks if it is a space. If it is, it also checks the previous character. If the previous character was also a space, it skips adding the current space to the new string. Otherwise, it adds the current character to the new string. This way, it only adds a single space even if there are multiple consecutive spaces.

I hope this helps! Let me know if you have any further questions or if this solution doesn't work for you. Good luck!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
7
Views
5K
Replies
31
Views
7K
Replies
1
Views
4K
Replies
43
Views
4K
Replies
2
Views
2K
Replies
1
Views
2K
Back
Top