A computer stores everything as numbers, and text is no exception. Every character has a number of its own, and a string is stored as the list of numbers for its characters. The code that says which number goes with which character is called ASCII, short for American Standard Code for Information Interchange.
You do not need to learn the codes by heart, but it is worth knowing how they are laid out:
| Characters | ASCII codes |
|---|---|
| a space | 32 |
the digits 0 to 9 |
48 to 57 |
the capital letters A to Z |
65 to 90 |
the small letters a to z |
97 to 122 |
The letters are in alphabetical order, so B is 66, C is 67 and so on up to
90 for Z. Capital letters and small letters have different codes: A is
65 but a is 97. That is why Python treats "A" and "a" as different
characters. Punctuation has codes too, mostly in the gaps between these ranges.
ord takes one character and gives back its ASCII code:
code = ord("B")
print(code)
That prints 66. The code is a whole number, not text, so it can be used in a
calculation straight away. To join it onto a string with +, wrap it in
str() first, as with any other number.
ord works on one character only. ord("Hi") stops the program with a
TypeError, because a two-character string does not have a single code. To get
the code of one character out of a longer string, pick that character out
first, as in ord(name[0]).
chr goes the other way. It takes a number and gives back the character with
that code:
letter = chr(66)
print(letter)
That prints B. The result is a one-character string, so it can be joined onto
other strings with +:
word = "" word = word + chr(67) word = word + chr(97) word = word + chr(116) print(word)
word starts out as the empty string and gains one character on each line, so
it spells out a word by the end. Look up 67, 97 and 116 in the table above and
work out what it will be before you run it.
Because the letters are numbered in order, adding to a code moves along the
alphabet. Go from the character to its code with ord, do the sum, then go back
to a character with chr:
letter = "c" code = ord(letter) next_code = code + 1 next_letter = chr(next_code) print(letter + " is followed by " + next_letter)
Both conversions are needed. letter + 1 is an error, because Python cannot add
a number to a string, and chr(letter) is an error too, because chr needs a
number.
The three lines can also be written as one, chr(ord(letter) + 1), which is
read from the inside out: the code of the letter, plus one, turned back into a
character.
ord fits into a loop like anything else. This one visits each letter of a
name with the short form of the for loop and keeps a running total of their
codes:
name = "Ada"
total = 0
for letter in name:
total = total + ord(letter)
print(total)
A is 65, d is 100 and a is 97, so the total is 262. Change name to
"ADA" and the total changes, because capital letters have smaller codes.
Every string operation in this chapter, in one table:
| What's it called? | What does it do? | Python code |
|---|---|---|
| Length | Returns the length of a string | len(string) |
| Concatenate | Joins strings and/or variables together | + |
| Substring | Returns part of a string | string[start_position:end_position + 1] |
| Substring | Returns characters from the left hand side of a string | string[:end_position + 1] |
| Substring | Returns characters from the right hand side of a string | string[start_position:] |
| Change case | Converts a string to upper case | string.upper() |
| Change case | Converts a string to lower case | string.lower() |
| Get ASCII number of a character | Returns the ASCII number of a character | ord(character) |
| Get character of an ASCII number | Returns the ASCII character of a number | chr(number) |
In the substring rows, end_position is the position of the last character
you want. The + 1 is there because a slice stops before its second number, so
it is what makes the last character part of the result.
Make sure that you check for the following things:
ord is given a single character and gives back a number.chr is given a number and gives back a single character.ord is wrapped in str() before it is joined onto text with a
+.A is 65 and a is 97.ord, add, and come back
with chr. Adding a number to a string directly is an error.