Week 3 : String value

-1 represent the last value of the string and 0 represent the first value of the string
lets take an example
variable1 = ‘exam’
print (variable1 [0] ) => represent e
print (variable1 [-1] ) =? represent m
but if we write below code it will only show : exa : wanted to understand why??
print (variable1 [0 : -1] )

This is because the last index is never included while slicing a string object in python.
For instance let’s say we have the following code snippet :

string_ = 'Data Science'
print(string_[1:6])

The output would be :

ata S

Which shows that the first index is included, whereas the last one is not.

1 Like