Strings And Numbers
Python supports several data types. In this lesson, you will learn more about number and string data types.
Learning Objectives
You should be able to:
- Describe how data types are used in programming
- Use the number and string data types in Python
Video Walkthrough
Use this video to follow along with the steps in this lab.
Numbers in Python
There are two kinds of numbers commonly used in Python: integers and floats. Integers are whole numbers. Floats are decimal numbers.
In Python, you do not need to declare the data type. This is in contrast to many other programming languages that force you to declare a data type prior to using the data. Python tries to figure out what kind of data you are supplying.
- Open a terminal.
- Run
pythonto start an interactive Python shell. - Run the following code.
print(4)
- Notice that an integer is output to the terminal. There are no decimal places. Python treats the number 4 like an integer without any decimal places.
- Run the following code.
print(5.9)
Python treats this number as a float--a number with decimal places.
- Run the following code.
5 / 3
The number 5 does not divide evenly by 3, so a float is produced (1.6666666666666667).
- If you really want an integer, you can use the
int()function to convert a number to an integer.
int(5 / 3)
The output will be the number 1. Python rounds down by default. So even though 1.67 is closer to 2, Python will output 1 in this case.
- Sometimes, you might want to work with decimal numbers. The
float()function can help.
float(4 / 2)
The results will be 2.0, even though 4 is evenly divisible by 2.
In many instances, you do not need to care about which of the two number data types Python is using. But when it matters, it really matters.
Strings in Python
Text data uses the string data type. The name string is common across most programming languages because text is made up of a "string of characters." String data can be put in single quotes or double quotes.
- Run the following code.
"Hello everyone."
The text will be repeated in the output, though with single quotes instead of double quotes.
- Try single quotes.
'Hi there'
Again, the text will be repeated, with single quotes.
- If you want to put an apostrophe in your text, the easiest way is use to use double quotes to surround the string.
"It's hammer time."
- If you try the above with single quotes, you'll get an error.
>>> 'It's hammer time.'
File "<stdin>", line 1
'It's hammer time.'
^
SyntaxError: invalid syntax
It is possible to "escape" the single quote using the backslash character (\). An escape character tells Python to treat the subsequent character as part of the string and not as code.
'It\'s hammer time.'
It is possible to escape double quotes as well.
"I said, \"No.\""
If you want to include double quotes in a string, you can surround the string with single quotes.
'"No," I said.'
You can use multiple lines for strings. Start and end the string with a triple quote.
"""Hello
there."""
Generally speaking, using double quotes is preferred to using single quotes for strings. But both work.
Numbers in Strings
If you put quotation marks around a number, Python will treat it as a string. The following two lines of code appear to produce the same output, but Python treats them differently.
print(5)
print("5")
We can try a little math to show how they differ. Run the following code and inspect the output.
print(5+1)
print("5" + 1)
print("5" + "1")
The first line will output 6. The second line will produce an error because the data types do not match. The third line combines the two strings to produce the output 51.
Occasionally, you might need to convert a string to a number. The int() and float() functions can help. Python will try to convert the string to a number so that you can do math.
print(int("5") + 1)
But to convert a string to a number, the string has to be a number. The following code will produce an error.
print(int("apple") + 1)
Numbers can be converted to strings using str(). In the following code, the + operator is used to combine two strings.
print(str(5) + " apples")
Exiting the Interpreter
When you are done editing your code, use exit() to close the Python interactive interpreter.
exit()
Reflection
- Why is a decimal number called a "float?" (Search online.)
Key Terms
- Data type - string: A data type used to represent sequences of characters. Strings are typically used to store and manipulate text. In many programming languages, strings are enclosed in quotes (single, double, or triple). Examples include
"hello",'world', and"12345". - Data type - integer: A data type used to represent whole numbers without any fractional or decimal component. Integers can be positive, negative, or zero. Examples include
-10,0, and42. - Data type - float: A data type used to represent numbers that have a fractional or decimal component. Floats are used for precise calculations involving real numbers. Examples include
3.14,-0.001, and2.71828.