It is hard for me to understand that what's going on in the following code. I am actually trying to remove the leading and trailing space from the string in python. The string has two leading and trailing spaces like this
" Hello World! "
I tried following simple code to remove the spaces
s = " Hello World! "
temp = s.split(" ")
//output of temp -> ['', '', 'hello', 'world!', '', '']
for x in temp:
if x == "":
temp.remove(x)
print(temp)
//output of temp -> ['hello', 'world!', '', '']
So it only removes the leading trailing spaces and not trailing. Can anyone explain why this function skipping the last two spaces?
Note: I don't want to use strip
function, just curious about what's going on under the hood