I have a string in the below format:
00:aa:bb:cc:dd:ee
I need to split it and output only first 3 fields, i.e. 00:aa:bb. I am using
s.split(':')[1:3]
in my program to do the job, but its not helping. Is it the right way to do this?
I have a string in the below format:
00:aa:bb:cc:dd:ee
I need to split it and output only first 3 fields, i.e. 00:aa:bb. I am using
s.split(':')[1:3]
in my program to do the job, but its not helping. Is it the right way to do this?
Almost: Python is 0-indexed (counts from 0):
>>> s = '00:aa:bb:cc:dd:ee'
>>> s.split(":")
['00', 'aa', 'bb', 'cc', 'dd', 'ee']
>>> s.split(":")[0:3]
['00', 'aa', 'bb']
>>> s.split(":")[:3]
['00', 'aa', 'bb']
And you could recombine them:
>>> ':'.join(s.split(":")[:3])
'00:aa:bb'
You can see this question for a brief review of how Python slicing works, if that's the issue.
What do you mean it's not helping? Is it because the result is a list? To turn it back to a string try:
':'.join(s.split(':')[0:3])
Also note the zero-index.