We can get substring from string (slicing) by following different ways:
- Specifying start and end index. a[startIndex: endIndex]
- Specifying just end index. a[:endIndex]
- Specifying just start index. a[startIndex:]
Specifying negative index will be equivalent to that index starting from end of the string.
myVar = "Hello World"
hello = myVar[0:5]
world = myVar[6:11]
sub1 = myVar[2:]
sub2 = myVar[:-3]
The output will be as follows:
Hello
World
llo World
Hello Wo