1
I’m manipulating strings and I came across this question. I thought the semantics of the ways to use was the same, but I saw that not and it left me confused.
In the case I thought with the use of split(" ")
all space in the original string would be removed and I would have something like:
['07', '10', '11', '20', '30', '44', '34 n']
But what I got on the way out was:
palavra = " 07 10 11 20 30 44 34\n"
splt = palavra.split(" ")
print(splt)
return:
['', '', '', '', '', '07', '10', '11', '20', '30', '44', '34 n']
And with the use of split()
came much closer than I expected.
palavra = " 07 10 11 20 30 44 34\n"
splt = palavra.split()
print(splt)
return:
['07', '10', '11', '20', '30', '44', '34']
Sorry, I don’t know if I got it right, so the
split(" ")
will only be removed actually when it is among other values, in my case the numbers. Thesplit()
will remove all being among other values or not, this?– Shinforinpola
@Shinforinpola
split(" ")
will separate the string in the spaces and keep everything you found (in your example, found "empty"), whilesplit()
will do the same thing at first and then discard these "voids".– Rafael Tavares