I’ve been trying to slice two characters out of a string using a loop, but instead of grabbing two characters, it only grabs one.
I’ve tried:
input[i:i+1]
and
input[i:(i+1)]
but neither seems to work.
How do I use a variable for slicing?
The full routine:
def StringTo2ByteList(input):
# converts data string to a byte list, written for ascii input only
rlist = []
for i in range(0, len(input), 2):
rlist.append(input[i:(i+1)])
return rlist
The slice values aren’t the start and end characters of the slice, they’re the start and end points. If you want to slice two elements then your stop must be 2 greater than your start.