Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
2.5. Data Types¶
If you are not sure what class (data type) a value falls into, Python has a function called type which can tell you.
What about values like
"3.2"? They look like numbers, but they
are in quotation marks like strings.
Strings in Python can be enclosed in either single quotes (
') or double
"), or three of each (
Double quoted strings can contain single quotes inside them, as in
beard", and single quoted strings can have double quotes inside them, as in
'The knights who say "Ni!"'.
Strings enclosed with three occurrences of either quote symbol are called
triple quoted strings. They can contain either single or double quotes:
Triple quoted strings can even span multiple lines:
Python doesn’t care whether you use single or double quotes or the three-of-a-kind quotes to surround your strings. Once it has parsed the text of your program or command, the way it stores the value is identical in all cases, and the surrounding quotes are not part of the value.
So the Python language designers usually chose to surround their strings by single quotes. What do you think would happen if the string already contained single quotes?
When you type a large integer, you might be tempted to use commas between
groups of three digits, as in
42,000. This is not a legal integer in
Python, but it does mean something else, which is legal:
Well, that’s not what we expected at all! Because of the comma, Python chose to treat this as a pair of values. In fact, a print statement can print any number of values as long as you separate them by commas. Notice that the values are separated by spaces when they are displayed.
Remember not to put commas or spaces in your integers, no matter how big they are. Also revisit what we said in the previous chapter: formal languages are strict, the notation is concise, and even the smallest change might mean something quite different from what you intended.
The examples in this online text describe how print works in Python 3. If you install Python 2.7 on your machine, it will work slightly differently. One difference is that print is not called as a function, so there are no parentheses around the values to be printed.
Check your understanding