Try This: Print Alphabets
The variable name s is often used in projects to name strings. In projects where many string operations are performed, s might be used repeatedly as a temporary or working variable that ends up being many different strings, each for a brief period of time. Strings in the project that have important meanings should be given descriptive names. One example would be password = “abc123”.
The variable name c can often be found as the name of a string if it stores a single character.
A built-in function is a block that’s always there in Makecode. For strings, Makecode has four important built-in blocks:
- char code from (“ ”) at () — Returns the ASCII value of a character
- text from char code () — Returns the character of an ASCII value
- length of () — Returns the length of a string
- convert () to text — Converts other types of objects to string
The first and second blocks were just used in the last example project, and you will get to experiment with the third and fourth blocks in another activity.
The first 32 ASCII codes are control characters intended for older printers and storage devices. Some of them are still used with terminals today. For example, ASCII character 10, the line feed character, causes the terminal’s cursor to move down a line. In the previous activity, you used the escape sequence \n to add the ASCII 10 to strings.
Printable characters range from 32 (space) to 126 (~). As you have seen from experimentation, the upper-case alphabet uses codes 65 through 90, the lower-case alphabet uses 97-122, and digits are codes 48-57.
Imagine a project that repeatedly calls the text from char code () block inside a for… loop. The first time, it prints text from char code (65), the second time, it prints text from char code (66), and continues all the way through text from char code (90). What do you think you’d see?
Example project: chars_in_strings_try_this
- Enter and name characters_in_strings_try_this.
- Click the Download button.
- Check the results in the serial terminal.
- Verify that the terminal displays the characters A through Z.
- For lowercase characters, try changing range start (65) end (90) step (1) to range start (97) end (122) step (1) and re-flashing the project. See the difference?
- How about characters that represent digits? Try range start (48) end (57) step (1).