6.2 Literals

A literal is a number or string written right into the program by the programmer, that is, literally typed right into the program’s code such as 42. We’ve already been using literals in most of the sample programs we’ve seen so far. When we use a function call like background(200), the 200 is a literal value that we’re using as an argument to the background() function.When we use a literal in our programs, Python needs to know the literal’s data type. We communicate the correct data type to Python by the way we write the literal, as follows:

Integer literals: Any number written without a decimal point is an integer literal. Thus, 42, -17, and 65535 are integer literals.

Floating-point literals: Any number written with a decimal point is a floating-point literal. Examples are: 42.0, -9.8, and 3.14159. Be careful that even the literal 42. (decimal point included) is a floating-point literal because it contains a decimal point. An empty sequence of digits after the decimal point is different from no decimal point at all! We can try this out in Python using the print() function:

If you run the program above, you’ll get 42.0 printed to the console.

Floating-point literals can also be written in scientific notation. For example, the speed of light is 3×108 m/s, a quantity that can be written as the literal 3e8. Again, we can try this with the print() function:

This program will print the value 300000000.0 to the console. Note the decimal point is preceding the rightmost zero in the sequence. Literals written in scientific notation are always floating-point, never integers.

String literals: Recall that in the previous section, we said that strings are sequences of characters. Python specifies a string literal by enclosing a sequence of characters with a pair of single or double-quotes. “Hello world.” and ‘The night is dark and full of terrors.’ are both examples of string literals. Strings can contain spaces because spaces are characters too. Any symbol that appears on the keyboard is a character. Note that there is a difference between the literals ‘7’ and 7; the former is a string literal and does not actually have the numeric value of 7, while the latter is an integer literal, which does. Similarly the literals “3.14159” and 3.14159 are different. The former is a string literal, which does not actually have the numeric value 3.14159, and the latter is a floating-point literal, which does. However, ‘Bazinga!’ and “Bazinga!” are exactly the same.
So why have two ways of writing string literals? It is so that we can conveniently include single or double quotes as part of a string (after all, the “ symbol is on your keyboard, so it’s also a character!). The string ‘The card says “Moops”.’ is enclosed in single quotes and contains two double quotes as part of the string. The single quotes are not part of the string; they’re special syntax that tells Python to interpret everything between them as a single piece of string data.

If we run the program above, the phrase The card says “Moops”. will print to the console. Notice that the print() function didn’t print the enclosing single quotes, because they weren’t part of the data.

By contrast, let’s look at what happens with the following program:

If you try to run this program in Processing, you’ll get an error. The reason for this is because Python interprets the characters between the first two double quotes (the first one and the one right before the word Moops) as a string literal. Then the word Moops makes no sense to Python because Python thinks the string literal is finished. So Python tries to interpret Moops as part of the Python language, which it isn’t, so Python doesn’t know what to do and gives up. The bottom line is that you can write single quotes inside string literals enclosed in double-quotes, and double quotes inside string literals enclosed in single quotes.

So should you use single or double quotes for strings? Well, there’s no right or wrong answer to this question. Unless you need single or double quotes within a string literal, it doesn’t matter. Typically, one chooses to use either single or double-quotes as one’s “default” style, and only uses the other when necessary.