Strings and numbers
Before starting with the course, I strongly recommend having your editor set up so you can code along and try things out.
Check out the previous chapter for more information!
What is a number?
A number in Python refers to any whole or decimal number on which you might want to do maths. Here's some examples of numbers:
- A person's age; for you might want to calculate the average age of a group.
- A professional's income; for you might want to calculate the total for the year.
- How many pages are in a book; for you might want to calculate how long it'll take a person to read it.
Numbers in Python are represented as the digits themselves, such as 56
, 87000.50
, or 360
.
What is a string?
A string, on the other hand, is not something you'll want to do maths with (but you can still use it for other stuff).
We use strings when we want to store and refer to specific letters and numbers. For example:
- A person's name.
- A person's date of birth; although you may want to use it to calculate age, you wouldn't do this using maths.
- A phone number; which is made up of digits but you would never want to add them up.
Strings in Python are represented as the characters we're interested in, surrounded by quotation marks—such as "Rolf"
, "1990-06-10"
, or "07248571374"
(not a real phone number, I hope!).
What's the difference between both—how can you tell them apart?
A good way to tell whether you want to use a number of a string is to think about whether you'll want to do maths with it.
What are different types of numbers?
The two types of numbers in Python are whole numbers and decimal numbers. These are known as integers and floats (for floating point, also known as decimal point). We refer to them within Python code as int
and float
.
There's a few other types as well, such as binary or hexadecimal numbers, but you won't encounter them very often.
What are different types of string?
In contrast, there's only one type of string. However, Python strings can be defined in two ways:
"Rolf"
; or'Rolf'
.
These two ways mean exactly the same thing, but you can use either.
You cannot mix and match the quotes, so "Rolf'
is not a valid string.
The point of having these two quotes is really just to make your life a bit easier. There'll be some occasions where you will want to use both types of quotation mark:
'He said "You are awesome".'
"He told me you're awesome."
In both these examples, we have a single string. The outer quotation mark, regardless of whether it's a single or a double, marks the string. The inner quotation mark is in both cases just another character stored within it.
Aim for consistency: pick one type of quotation mark, and always use that one—unless you find yourself in a situation where you need to use a different one!
We can avoid using different quotation marks by escaping them inside the string. However, it looks a bit ugly and I wouldn't recommend it. Here's the strings above using escaping instead of changing the quotation marks:
"He said \"You are awesome\"."
'He told me you\'re awesome.'
How do mathematics work in Python?
Mathematics order works the same way in Python as it does in real life—we follow rules known as BODMAS or PEMDAS:
- Brackets (or Parentheses)
- Order (or Exponents)
- Division & Multiplication
- Addition & Subtraction
These calculations evaluate just as in real life:
5 + 6 * 2 / 4
evaluates to8.0
.(4 + 6) / 3
evaluates to3.3333333333333335
.2 ** 4 * 3
evaluates to48
.
Two things you may have noticed there:
- Whenever you use division you end up with a
float
, even if the result is a whole number. - The symbol for exponents in Python is
**
, so2 ** 4
is 2 to the power of 4, or2 * 2 * 2 * 2
.
Adding strings and numbers
In Python we cannot add a string and a number. If we try to do this, we'll always get an error:
"Rolf" + 30
You can however add two strings, and that gives you a new string composed of the two strings you added joined together:
"Rolf" + "Smith"
The result would be "RolfSmith"
.
If you wanted the result to be "Rolf Smith"
(with a space between the name and surname), you can always add spaces within one of the strings.
For example, "Rolf " + "Smith"
.
You could also do "Rolf" + " " + "Smith"
.
We will learn more about joining strings together easily later on in this section.
Extra resources
The official Python documentation is good, but very verbose. Don't feel the need to read it all but you can check it out for some more in-depth detailed explanations: