Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"/1/2/3/".split("/")

Tags:

python

string

It's too hot & I'm probably being retarded.

>>> "/1/2/3/".split("/")
['', '1', '2', '3','']

Whats with the empty elements at the start and end?

Edit: Thanks all, im putting this down to heat induced brain failure. The docs aren't quite the clearest though, from http://docs.python.org/library/stdtypes.html

"Return a list of the words in the string, using sep as the delimiter string"

Is there a word before the first, or after the last "/"?

like image 443
sjh Avatar asked Nov 29 '22 04:11

sjh


1 Answers

Compare with:

"1/2/3".split("/")

Empty elements are still elements.

You could use strip('/') to trim the delimiter from the beginning/end of your string.

like image 169
ceejayoz Avatar answered Dec 10 '22 10:12

ceejayoz