Some of today's topics are covered in these sections of Think Python:
8. Strings
And in these chapters of Introducing Python:
Chapter 5. Text Strings
Chapter 6. Loop with while and for
Here are some additional notes.
Computers generally store text using a coded character set, which assigns a unique number called a code point to each character. Two character sets are used in virtually all software systems today:
The ASCII character set includes only 128 characters; its code points range from 0 to 127. For example, in ASCII the character 'A' has the number 65, and 'B' has the number 66. ASCII includes all the characters you see on a standard English-language keyboard: the uppercase and lowercase letters A-Z/a-z of the Latin alphabet, the numbers 0-9 and various punctuation marks such as $, % and &. ASCII does not include accented characters such as č or ř.
The newer Unicode character set extends ASCII to include all characters in all languages of the world, including accented characters and also ideographic characters in Asian languages such as 日. Code points in Unicode range from 0 to 1,114,111.
Python 3 is fully compatible with Unicode. You can write
s = 'Řehoř'
or
s = '人'
and these strings will work just like strings of ASCII characters.
Python includes two functions that can map between characters and their integer code points.
Given a Unicode character c, ord(c) returns its code point. For example, ord('A') is 65, and ord('B') is 66. ord('ř') is 345 (a value outside the ASCII range).
chr() works inversely: it maps a code point to a character. For example, ord(65) is 'A', and ord(345) is 'ř'.
These functions are sometimes useful when we wish to manipulate characters. For example, here is a program that reads a lowercase letter, and prints the next letter in the alphabet:
c = input('enter letter: ') i = ord(c) - ord('a') if 0 <= i < 26: i = (i + 1) % 26 print('next letter is', chr(ord('a') + i)) else: print('not a lowercase letter')
The program uses ord() to convert a character (such as 'd') to a number (such as 3) representing its position in the lowercase alphabet. It then adds 1 (mod 26), and uses chr() to map the result back into a lowercase letter.
Python includes both functions and methods.
A function takes one or more arguments and optionally returns a value. Some of Python's built-in functions that we've already seen in this course are len(), chr(), ord(), input() and print(), for example. To call a function, we simply write its name followed by the arguments:
n = input('Enter a number: ')
A method is like a function, but is invoked on a particular object. For example:
s = 'yoyo' b = s.startswith('yo') # method call
In the second line above, we are invoking (or calling) the startswith() method on the object s. (Recall that in Python a value and an object are the same thing.) This method call takes one additional argument, namely the string 'yo'. It returns a value (True in this case).
Both functions and methods are common in programming languages today. However, they are not both present in all languages. Many procedural languages (including C and classic Pascal) and functional languages (including Haskell) have only functions, not methods.
On the other hand, some object-oriented languages (including Java and C#) have only methods, not functions.
Python is a bit of a hybrid since it has both functions and methods. This arguably makes the language more flexible and convenient at the cost of some complexity.
In this course we will soon learn how to write our own functions, and before long (in a few lectures) will learn how to write our own methods as well.