Strings and Numbers

You might not realise, but you’ve already seen strings and integers in action. So what are they?

Try typing this into the shell:

print ('Python can do math')
print (56 + 4)

What’s the result? It’s not surprising really, but it prints Python can do math on one line, and 60 on another line.

We can see two things from this which are really interesting. The first one is that we can use the print function to print numbers, as well as do math before they’re printed.

The second is that sometimes we use quote-marks, and sometimes we don’t.

In Python, as well as all other programming languages, there are different types of information. These are called data types. When we printed 56 + 4, we were using numbers. Whole numbers like these are called integers. There are other types of numbers, but we don’t need to worry about that right now.

A whole number is called an ‘integer’

If something is between quote-marks, like ‘Python can do math’, this is a string. A string is just text. Think of it as a string of characters that are put together to make a sentence.

A ‘string’ is a string of characters, making up text

What would happen then, if we typed this?

print ('60')

Even though we know that 60 is a number, we’ve told python it’s a string. This particular string is two characters long (a ‘6’ and a ‘0’). This is completely fine, and Python will still print it.

So does it even matter if we use strings or integers? Try this:

print ('56 + 4')

This is very similar to the example from earlier. Notice the difference though? We’ve included quote-marks, which makes this a string.

The result is that Python prints this to the screen exactly as we’ve written it. Python doesn’t see these as numbers anymore, so it won’t add them together.

So it’s important to know if we’re using strings or numbers.

Leave a Reply Cancel reply