⭐ 1.5.6 Operation on Text

View

The text operators in Scratch can work with user-entered text, variables containing text, and just plain text to figure out how many characters are in it, find certain letters in the text, and join pieces of text together.

Combining Text with join () ()

The join () () block lets you combine words, and even sentences, to create custom values to store in variables or to be used by sprites.  A very common use for the join () () block is for mixing the values of variables into things that will be said by a sprite.

Activity: Cat counting potatoes

In this activity, you will let the cat count potatoes until a number stored in the variable called potatoes is reached. Follow the steps to perform this activity:

  1. Click File  new in the top toolbar to create a new project.

 

  1. In the Data Block Palette, create a new variable called potatoes.

 

  1. Drag a” when green flag clicked” block from the Events Block Palette to the Scripts Area .
  1. Drag the set () to () block from the Data Block Palette and snap it to the when green flag clicked block. 

 

  1. Make sure that the drop-down menu in the set () to () block is set to potatoes, and that the second space in the block contains a 0.
  1. Drag a repeat () block from the Control Block Palette and snap it to the bottom of the set (potatoes) to (0) block.
  1. Change the value in the repeat () block to 4.

 

  1. Drag a change () by () block from the Data Block Palette and snap it inside the repeat () block.  The drop-down menu should be set to potatoes and the second space should be set to 1.
  1. Drag a say () for () secs block from the Looks Block Palette and snap it to the bottom of the change (potatoes) by (1) block inside the repeat (4) block. 

  1. Drag a join () () block from the Operators Block Palette into the first space in the say () for () secs block.
  1. Drag the potatoes variable from the Data Block Palette and place it inside the first space in the join () () block.
  1. Type the word potato into the second space in the join () () block.
  1. Click the Green Flag to start the counting.

The result on the Stage should look like Figure 33.

Figure 33: Joining a variable and text

You are almost there, but it is strange how Scratch the Cat does not have a space between the number and the word potato. It is important to remember that Scratch does not add spaces for you when you join words together. 

To fix the problem, click your mouse just before the word potato in the second space in the join () () block and press the spacebar.  Now click the Green Flag again, and it looks much better, as shown in Figure 34.

Figure 34: Do not forget to use spaces!

Finding Letters

The letter () of () block tells you what letter comes at a certain position in a word or text. In this activity, you will create a block which tells the first letter of a word.

  1. Drag a letter () of () block from the Operators Block Palette to the Scripts Area.
  2. All coding languages start counting from (0) so, you can choose which number to enter. For this, enter the number 1 into the first space and your name into the second space in the block.
  3. Double-click the block.

You see a talk bubble with the first letter of your name in it (Figure 35).

Figure 35: Finding out the first letter in your name

When you combine the letter () of () block with the length of () block, you can do some nice things. Try it out.

Getting the length of a text

The length of () block tells you how many characters (including spaces and punctuation) there are in a piece of text.  If you want to find the last letter of a person’s name, you can combine the length of () block with the letter () of () block. Try it out:

  1. Drag a letter () of () block to the Scripts Area.
  2. Drag a length of () block to the Scripts Area and place it inside the first space in the letter () of () block.
  3. Type your name into both the length of () block and the letter () of () block (Figure 36).
  4. Double-click this block to find out the very last letter in whatever text you entered.

Figure 36:  A block to get the last letter of a name

When Scratch runs this combined block, it first determines the result of the length of () block. It then uses this number to determine what letter is in that position. In the preceding example, the result of the length of () block is 5 and the result is the letter S because S is the fifth letter in the name Chris.

 


Last modified: Thursday, 23 September 2021, 12:33 PM