What is string slice? How is it used?

 

Answer:

The substring obtained from within a string is called a string slice .

Following are the steps how it is used:

  • Given a string str1, the slice operation str1[n:m] returns the part of the string str1 starting from index n (inclusive) and ending at m (exclusive). 
  • In other words, we can say that str1[n:m] returns all the characters starting from str1[n] till str1[m-1]. 
  • The numbers of characters in the substring will always be equal to the difference of two indices m and n, i.e., (m-n).
Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.