Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split() not working?

So I am trying to split a string s:

s = "l=2&w=3&h=2"

However whenever I try to use the split() function on s and store the values in list L, this comes up:

L = s.split()
L --> ['l=2&w=3&h=2']

Am I doing something wrong? How do I split this string so I get:

L = ['l','=','2','&','w','=','3','&','h','=','2']
like image 777
Tahmoor Cyan Avatar asked Sep 18 '26 17:09

Tahmoor Cyan


2 Answers

It's actually easier than you might think.

L = list(s)

In Python, strings are iterable, just like lists. If you just need to iterate over the string, you don't even need to store it in a list.

like image 169
Cody Piersall Avatar answered Sep 20 '26 05:09

Cody Piersall


split() with no arguments splits on whitespace, which your string contains none of. To split on every character, just convert your string directly to a list:

L = list(s)
like image 43
jwodder Avatar answered Sep 20 '26 07:09

jwodder