String Slicing

  • Through string slicing, we can access a part of the string using indexes
  • Each character in a string gets assigned a index, starting from 0
  • Syntax in Python is [startindex:endindex]
  • Syntax in JS is .slice(startindex, endindex)
print("Hello Brawler"[0:5])
Hello
%%js
console.log("Hello Brawler".slice(0, 5));
<IPython.core.display.Javascript object>
  • Use Case: Extract substrings, like the first word from a sentence