DEV Community

Suresh S
Suresh S

Posted on

Indexing and Slicing

  • Indexing and slicing is important concepts for me.
  • Possible application is LSB and MSB in digital electronics

Application:
I have a device which sets temperature based on the float value I set from driver or user interface. Internally float value converted into hex and device is asked to set the required temperature.

But When we want to read the complete details from device, it will be in hex format with multiple bytes. If I need only one byte of data in reverse order of its binary value, then slicing is best way to extract that value.

But I think Python is more efficient. When I learn more concepts, then some package related to hex to binary or dealing with hex datas would have some functions.

Learning:
I have tried and learnt the below items from this session

  • Positive indexing
  • Negative indexing
  • Slicing using positive indexing
  • slicing using negative indexing
  • Slicing in reverse order requires third argument Step
###############################################
#         INDEXING
###############################################

'''
Positive Indexing:
H E L L O   W O R L D
0 1 2 3 4 5 6 7 8 9 10
'''

#Positive indexing
message = 'HELLO WORLD'
print ('Postive indexing:', message[0], message [1], message [2], message [3], message [4])  # H E L L O

'''
Here indexing starts with 0. Python is able to strip the string like array elements
Negative indexing
H   E   L   L   O
-5  -4 -3  -2  -1
'''

#Negative indexing
message1 = '  Hello'
print ('Negative Indexing:', message1[-1], message1[-2], message1[-3], message1[-4], message1[-5]) # o l l e H

'''
Length is always number of characters or elements in string. 
  - length > last element index
  - length = last index +1

when we define out of range indexing, string index out of range error would come
In the above example,
'''

print('Length of string:',len(message), len(message1))     # 11 , 7


###############################################
#         SLICING
###############################################

#Message[Start:Stop:Step]
print('\nSlicing 1 to 4th index elements of HELLO WORLD using message[1:5]:', message[1:5])
print('Slicing before 6th index elements of HELLO WORLD using message[:6]:', message[:6])
print('Slicing from 6th index elements of HELLO WORLD using message[6:]:', message[6:])
print('Slicing from 6th index elements of HELLO WORLD using message[6: 100]:', message[6:100])


# Slicing using negative index also possible
print('\nSlicing using negative indexing in HELLO WORLD using message[-11:5]:', message[-11:5])
# Here number 5 is STOP, it refers 5th index
print('Slicing using negative indexing in HELLO WORLD using message[-11:-4]:', message[-11:-4])

'''
Reversing the message contents can be done using step definition
message [5:-10:-1]
'''
print('\nSlicing in reverse order using step (message [5:-12:-1]):',message [5:-12:-1])
print('Slicing in reverse order only ROW(NI) from HELLO WORLD (message [-3:-7:-1]):',message [-3:-7:-1])
print('Slicing in reverse order only ROW(PI) from HELLO WORLD (message [8:4:-1]):',message [8:4:-1])

Enter fullscreen mode Exit fullscreen mode

Result:

PS C:\Projects\PythonSuresh> python Class7.py
Postive indexing: H E L L O
Negative Indexing: o l l e H
Length of string: 11 7

Slicing 1 to 4th index elements of HELLO WORLD using message[1:5]: ELLO
Slicing before 6th index elements of HELLO WORLD using message[:6]: HELLO 
Slicing from 6th index elements of HELLO WORLD using message[6:]: WORLD
Slicing from 6th index elements of HELLO WORLD using message[6: 100]: WORLD

Slicing using negative indexing in HELLO WORLD using message[-11:5]: HELLO
Slicing using negative indexing in HELLO WORLD using message[-11:-4]: HELLO W

Slicing in reverse order using step (message [5:-12:-1]):  OLLEH
Slicing in reverse order only ROW(NI) from HELLO WORLD (message [-3:-7:-1]): ROW
Slicing in reverse order only ROW(PI) from HELLO WORLD (message [8:4:-1]): ROW
Enter fullscreen mode Exit fullscreen mode

Top comments (0)