Python allows the user to :
operator.
The syntax to slice a string is as follows.
string[start: end: step]
start
indicates the starting index from where the string needs to be spliced.
end
specifies the index of the string until which splicing needs to be done.
step
specifies the number of steps needed to jump from the current index to the next index of the string.
If
step
is not specified, it stays at 1.
The string
is initially spliced in the code below by specifying the starting and ending index.
With the step
value set to 2, the string is extracted by skipping every character after the starting index.
If the starting index is not given, the splicing has to start from index 0.
Similarly, if the ending index is not given, the splicing needs to be done until the end of the string.
# splicing without specifying any stepstring = "Educative"x = string[2:4]print (x)# splicing with a specified stepy = string[0:7:2]print (y)# splicing without specifying the starting indexz = string[:3]print (z)# splicing without specifying the ending indexa = string[3:]print (a)