Find a string

  • + 1 comment

    b = substring i : current index len(b): length of substring let s = abcdcdc b = cdc len(b) = 3 Iteration be like i = 0 s[0:3] == b => abc ==cdc false i = 1 s[1:4] == b => bcd ==cdc false i = 2 s[2:5] == b => cdc == cdc True i = 3 s[3:6] == b => dcd == cdc False i = 4 s[4:7] == b =>cdc == cdc True After that iteration should stop as the complete string is search. Hence loop should go from len(s) - len(b) +1 For More information learn about string slicing and range function in python.