Lesson 5: Python Basics 3 - Formatting#
This lesson is modified from Basic elements of Python from geo-python.
Objectives#
By the end of this lesson, you will be able to
work with both text and numbers togehter
format text and numbers in python
use f-formatting to print text with numbers with decimal, scientific notation, and percentage
1. Making different data types work together#
In the previous lesson we learned how to determine a variable’s data type
. Here we will learn how to enable data of different types to work together.
1.1 Reminder: Data types and their compatibility#
We can explore the different types of data stored in variables using the type()
function.
Let’s use the cells below to check the data types of the variables station_name
, station_id
, and station_lat
.
station_name = "Immokalee"
station_id = 440
station_lat = 25.5126
station_lon = 24.96
station_county = "Collier"
type(station_name)
str
type(station_id)
int
type(station_lat)
float
As expected, we see that the station_name
is a character string, the station_id
is an integer, and the station_lat
is a floating point number.
Hint
Remember, the data types are important because some are not compatible with one another.
What happens when you try to add the variables station_name
and station_id
in the cell below?
station_name + station_id
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 station_name + station_id
TypeError: can only concatenate str (not "int") to str
Here we get a TypeError
because Python does not know to combine a string of characters (station_name
) with an integer value (station_id
).
1.2 Converting data from one type to another#
It is not the case that things like the station_name
and station_id
cannot be combined at all, but in order to combine a character string with a number we need to perform a data type conversion to make them compatible. Let’s convert station_id
to a character string using the str()
function.
station_id = str(station_id)
We can confirm the type has changed by checking the type of station_id
, or by checking the output when you type the name of the variable into a cell and run it.
type(station_id)
str
station_id
'440'
Notice the number is now enclosed in quotation marks. As you can see, str()
converts a numerical value into a character string with the same numbers as before.
Now you can do station_name + station_id
without an error
station_name + station_id
'Immokalee440'
Note
Similar to using str()
to convert numbers to character strings, int()
can be used to convert strings or floating point numbers to integers and float()
can be used to convert strings or integers to floating point numbers.
Try converting station_id
back to integer
station_id=int(station_id)
station_id
440
1.3 Combining text and numbers#
Although most mathematical operations operate on numerical values, a common way to combine character strings is using the addition operator +
. Let’s create a text string in the variable station_name_and_id
that is the combination of the station_name
and station_id
variables. Once we define station_name_and_id
, we can print it to the screen to see the result that should look like this
Output: Immokalee: 440
Note you need to convert station_id
to a character string using the str()
function within the assignment to the variable station_name_and_id
.
station_name_and_id = station_name + ": " + str(station_id)
print(station_name_and_id)
Immokalee: 440
2. Working with text (and numbers)#
The previous example demonstrated how it is possible to combine character strings and numbers (converted to character strings) together using the +
operator. Although this approach works, it can become quite laborous and error-prone when working with more complicated sets of textual and/or numerical components. In addition, it is sometimes desirable to format the numerical output to change the number of decimal places for floating point values, for example. Hence, next we show a few useful techniques that make manipulating strings easier and more efficient.
There are three approaches that can be used to manipulate strings in Python:
f-strings
the
.format()
methodusing the
%
operator
The f-string approach is recommended and the most modern, introduced in Python 3.6. However, since you are likely to find examples of the older approaches we also show how they work.
2.1 f-String formatting#
Here, we show how we can combine the station_name
text, station_id
integer number and another floating point number temp
using Python’s f-string formatting approach. In addition, we will simultaneously round the floating point number (temp
) to two decimal places. We want an output that looks like this
Output: The temperature at Immokalee station (ID: 440) is 18.57 degrees Celsius.
Here is how to do this using f-formatting
#Here are the variables that you will use
station_name="Immokalee" #An example of string variable
station_id= 440 #An example of integer variable
temp = 18.56789876 # An example float variable with many decimals
# The f-string approach (recommended)
info_text = f"The temperature at {station_name} station (ID: {station_id}) is {temp:.2f} degrees Celsius."
print(info_text)
The temperature at Immokalee station (ID: 440) is 18.57 degrees Celsius.
So, here we have managed to combined three different data types and format the floating point value in a single line. Let’s break the f-string down a bit to understand how it works.
f-string formatting explained. Image from the draft text of the Introduction to Python for Geographic Data Analysis textbook by Tenkanen et al..
The key components here are:
The text that you want to create and/or modify is enclosed within the quotes preceded with letter
f
.You can include any existing variable in the text template by placing the name of the variable inside a set of curly braces
{}
.Using string formatting, it is also possible to insert numbers (such as
station_id
andtemp
) into the body of text without needing first to convert the data type to a string. This is because the f-string functionality does the data type conversion for us.
It is possible to round numbers on the fly to a specific precision, such as two decimal points as in our example by adding format specifier (
:.2f
) after the variable that we want to format.The format specifier works by first adding a colon (
:
) after the variable nameThe decimal precision can be specified by adding a dot (
.
) followed by a number that indicates the number of decimal places (two in our case)The final character
f
in the format specifier defines the type of the conversion that will be conductedf
will convert the value to decimal numbere
orE
will make the number appear in scientific notation%
will convert the value to a percentage
Of the above, the most important thing is to remember to include the f
at the start of your f-strings :).
Test your understanding#
Given the variables defined below, print the following sentence to screen:
Output: The concentration of carbon dioxide in the atmosphere has increased by more than 40% since the pre-industrial era, reaching 415.79 ppm in 2021, about 4.0E-02 percent of the atmosphere.
Note The line will get long, to contine typing in a new line you need to add \
to continue typing in a new line
#Here are the variables that you will use
name="carbon dioxide" #string
increase_rate=0.4 #this will be converted to percentage using %
concentration=415.78888 #this will be shown with only two decimal numbers
year=2021 #intger
CO2_ratio=0.04 #this will be shown in scientific notation
print(f"The concentration of {name} \
in the atmosphere has increased by more than {increase_rate:.0%} \
since the pre-industrial era, reaching {concentration:.2f} ppm in {year}, \
about {CO2_ratio:.1E} percent of the atmosphere" )
The concentration of carbon dioxide in the atmosphere has increased by more than 40% since the pre-industrial era, reaching 415.79 ppm in 2021, about 4.0E-02 percent of the atmosphere
2.2 .format()
approach (no longer recommended)#
As mentioned above, there are other approaches that can be used to format text and combine different data types. The first one is the .format()
method. For example:
info_text2 = "The temperature at {0} station (ID: {1}) is {2:.2f} Celsius.".format(station_name, station_id, temp)
print(info_text2)
The temperature at Immokalee station (ID: 440) is 18.57 Celsius.
As you can see, here we get the same result as with f-strings using the .format()
method, which is placed after the quotes. Placeholders are inserted inside curly braces where the numbers refer to the order of the variables listed in the .format()
function. There are other ways to use this same approach, but the example above is typical. For example, you can omit the numbers and in this case variable will be taken in order.
info_text2 = "The temperature at {} station (ID: {}) is {:.2f} Celsius.".format(station_name, station_id, temp)
print(info_text2)
The temperature at Immokalee station (ID: 440) is 18.57 Celsius.
2.3 %
operator approach (no longer recommended)#
The last (historical) string formatting approach is to use the %
operator. In this approach, the placeholder %
is added within the quotes, and the variables that are inserted into the body of text are placed inside parentheses after another %
operator, like this:
info_text3 = "The temperature at %s station (ID: %s) is %.2f Celsius." % (station_name, station_id, temp)
print(info_text3)
The temperature at Immokalee station (ID: 440) is 18.57 Celsius.
The order of the variables within the parentheses specify which %
placeholder will receive what information and the number of variables should be exactly the same as the number of %
placeholders within the text template.
2.4 More information about formatting text and numbers#
Of course, there is much more that can be done to format and interact with character strings and numbers. For more information, please have a look at the sites linked below.