Data structures and algorithms · Lesson 16 of 60 · Code lesson, JavaScript or Python · Optional
Strings and text processing
A string is text, and each character has an index just like an item in an array.
You can slice out a piece, search for text inside it, and format it, for example by changing the case.
Under the hood each character is stored as a number using an encoding such as Unicode.
Worked example
In JavaScript, word.slice(0, 3) takes the first three characters, and word.toUpperCase() changes the case.
const word = "banana";
console.log(word.slice(1, 4));Output: ana
In Python, word[:3] takes the first three characters, and word.upper() changes the case.
word = "banana"
print(word[1:4])Output: ana
Your turn
The program reads a word. Make it print only the first three letters, in capital letters.
Your program is checked with these 3 inputs:
Input:
banana
Expected output:
BAN
Input:
kiwi
Expected output:
KIW
Input:
ab
Expected output:
AB
Loading your practice…
Preparing your workspace…