Hey there! Ready to explore the world of coding? Today, we will learn how computers handle text using strings!
In coding, any text inside quotation marks is called a string. It can be a single letter, a whole word, or even a full sentence with spaces and punctuation.
We can glue strings together using a process called concatenation. Think of it like snapping toy train cars together to build a longer train.
Every character in a string has an address called an index, starting at 0. We can find the length of a string by counting all its characters, including spaces!
✏️ Worked example
Let's build a smart greeting! We want to take a user's name, like "Sam", and turn it into a friendly shoutout: "** SAM **". How do we design a program to do this step-by-step?
First, we find the input name. Let's use the string variable: name = "Sam".
Next, we use a built-in string method to change all the letters to uppercase. This turns "Sam" into "SAM".
Now, we want to add decorative stars to the left and right. We create a new string variable containing the star pattern: stars = "** ".
Finally, we glue them together using concatenation. We add stars + name + stars to get our final output: "** SAM **".
Want the full interactive lesson — quiz, animations, and Finn cheering them on?