Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.4 String Operations
3.4 Team Teach String Operations
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