Each string is a sequence of characters, where each character is defined by a code number. For example, the string "ABC 123" is stored as a sequence of numbers: 65 65 66 67 32 49 50 51 66 49 50 51. These numbers are called ASCII codes. ASCII stands for American Standard Code for Information Exchange.
You can use Python statements to return the ASCII code of a character, and also to return the character for a given ASCII code. Let's try that!
# chars_in_strings_intro from microbit import * sleep(1000) print("n = ASCII code for A") n = ord("A") print("n =", n) print() print("c = character with ASCII code 66") c = chr(66) print("c =", c) print()
The statement print("n = ASCII code for A") displays a heading to help give context to the messages below it.
print("n = ASCII code for A")
The function ord() returns the ASCII code of a string that contains a single character. The ASCII code for the character A is 65. So, ord("A") returns 65, and n = ord("A") stores the value 65 in the variable n.
n = ord("A")
After that, print("n = ", n) displays "n =", followed by the value of n, which is 65. Lastly, the print() just prints an empty line.
print("n =", n) print()
Note: the ord() function only works if the string contains a single character! The MicroPython runtime doesn't care whether you enclose characters in single or double-quotes, but single quotes for single characters are more readable. Consider "A" vs. 'A'.
After that empty line, print("c = character with ASCII code 66") prints another heading.
print("c = character with ASCII code 66")
The chr() function accepts an ASCII code, and returns that ASCII code's character. So, c = chr(66) stores the character B in a variable named c.
c = chr(66)
Since c now stores the character B, print("c =", c) prints c = B.
print("c =", c) print()
The variable name s is often used in scripts to name strings. In scripts 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 script 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 one that's always there in Python, no module importing required. For strings, Python has four important built-in functions:
The ord() and char() functions were just used in the last example script, and you will get to experiment with len() and str() 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 Python 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 is codes 65 through 90, the lower-case alphabet is 97-122, and digits are 48-57.
Imagine a script that repeatedly calls the chr() function inside a for... loop. The first time, it prints chr(65), the second time, it prints chr(66), and continues all the way through chr(90). What do you think you'd see?
# chars_in_strings_try_this from microbit import * sleep(1000) for n in range(65, 91): c = chr(n) print(c) sleep(250)
You can print your own list of printable ASCII characters with the next example script.
# chars_in_strings_your_turn from microbit import * sleep(1000) print("ASCII") print("Code Character") for n in range(32, 127): c = chr(n) print(n, " ", c) sleep(250)
Links
[1] https://learn.parallax.com/support/reference/ascii-table-0-127