-3

I have the following data in a huge string:

data="in West upto 19:17         
 
 
in North from 19:17   "

I am trying to remove "'&nbsp ;'" from this huge string but it does not work with strip like this:

data= data.strip(' ')

Same output as before no change in text of data:

print(data)

in West upto 19:17         
 
 
in North from 19:17 

Why is there no change no stripping is happening all the &nbsp are present there and then?

Help Please!

Puneet Mathur
  • 79
  • 2
  • 9

2 Answers2

3

str.strip() removes characters from the beginning of the line or the end of the line only. It does not modify those in between, so instances of the substring   situated elsewhere in the string will not be removed. Also, str.strip() removes any of the characters in the given string, not the actual string.

You can use str.replace() to remove the substring ' ':

data = data.replace(' `, '')
mhawke
  • 84,695
  • 9
  • 117
  • 138
1

What you are probably looking for is replace rather then strip is used to eliminate characters at the beginning or end of your text. By default it is removing spaces, but you can specify a character as well.

data.replace("&nbsp", "")
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Please add some description. As it is your answer might be flagged as low quality. – Klaus D. Nov 25 '17 at 10:05
  • This works I am unable to accept your answer as there is a Bot made wait of 6 minutes. Will accept once this time expires. – Puneet Mathur Nov 25 '17 at 10:07
  • @KlausD. It does not matter if an answer is low quality or high it should work and this answer works for me. – Puneet Mathur Nov 25 '17 at 10:08
  • Sounds good, If you think other answers were usefull too, you can upvote them and make people happy :D – user1767754 Nov 25 '17 at 10:09
  • 2
    @PuneetMathur: you should prefer the correct answers of higher quality. Those tend to be answers with some explanation. Also, your question does not ask for a solution, it asks for an _explanation_ as to _why_ `strip()` did not work as you expected. This answer (originally) did not provide that explanation. – mhawke Nov 25 '17 at 10:19
  • agree in this answer there is no explanation and there is a mistake with a missing ";" – Puneet Mathur Nov 25 '17 at 12:09