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']
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With