Problem Formulation and Solution Overview
βΉοΈ Interpolation is used when a string contains a placeholder for a variable or a calculation. The string is evaluated, and the placeholder is replaced by the passed data.
To make it more interesting, we have the following running scenario:
- Method 1: Use
template()
andsubstitute()
- Method 2: Use
f-string
- Method 3: Use the % operator
- Method 4: Use
format()
- Bonus: Putting it Together
Method 1: Use template() and substitute()
This method calls string.Template()
and uses a dollar sign ($
) and a variable name to define a placeholder ($first_name
). Whereas substitute()
replaces that placeholder with the specified data.
import string clients = ['Bill', 'Micah', 'Philip', 'Josiah'] salutation = string.Template('Dear $first_name,\n') for fname in clients: print(salutation.substitute(first_name=fname))
The first line in the above code snippet imports the string
library. This is required because string.Template()
and substitute()
are being called.
The following line declares a list
of client names. The results save to clients
.
The highlighted line declares string.Template()
and passes this one (1) argument: the formatted salutation
. This string consists of the word Dear
and a placeholder, $first_name
, followed by a comma and a newline character (,
\n).
In the following two (2) lines, a for
loop iterates through this list
, substituting the salutation
placeholder with the client’s first name. The results are then output to the terminal.
Dear Bill,
Dear Micah,
Dear Philip,
Dear Josiah,
Method 2: Use f-string
This method uses an f-string
with the curly braces ({}
) as a placeholder. This placeholder is replaced with the specified data.
clients = ['Bill', 'Micah', 'Philip', 'Josiah'] for fname in clients: print(f'{fname}, I just wanted to touch base with you.\n')
The first line in the above code snippet declares a list
of client names. The results save to clients
.
In the following two (2) lines, a for
loop iterates through this list
, substituting the {fname}
placeholder with the client’s name followed by a period and a newline character (.\n
). The results are then output to the terminal.
Bill, I just wanted to touch base with you.
Micah, I just wanted to touch base with you.
Philip, I just wanted to touch base with you.
Josiah, I just wanted to touch base with you.
π‘Note: In this example, the f-string is called inside the print()
statement to indicate to Python to expect a placeholder substitution and output the results to the terminal.
Method 3: Use the Percentage Operator %
The %
operator, although an older method, still works and can be used as a placeholder.
m_rates = [3.725, 4.122, 5.398, 2.409] for r in m_rates: print('Your current mortgage rate is %.3f.\n' % (r))
The first line in the above code snippet declares a list
of mortgage rates associated to the clients
list
created earlier. The results save to m_rates
.
In the following two (2) lines, a for
loop iterates through this list
, substituting the %
placeholder with the client’s mortgage rate (with 3 decimal places) followed by a period and a newline character (,
\n). The results are then output to the terminal.
Your current mortgage rate is 3.73.
Your current mortgage rate is 4.12.
Your current mortgage rate is 5.40.
Your current mortgage rate is 2.41.
Method 4: Use format()
This method uses empty curly braces({}
) as a placeholder and calls format()
to identify the field(s) to replace with the specified data.
clients = ['Bill', 'Micah', 'Philip', 'Josiah'] new_rate = 7.876 for fname in clients: print('Your mortgage is renewing soon, and the new rate is {}%.\n'.format(new_rate))
The first two lines in the above code snippet declare a list
of client names and the new mortgage. These save to clients
and new_rate
, respectively.
In the following two (2) lines, a for
loop iterates through the client
list
, substituting the ()
placeholder with the client’s new rate followed by a percent sign, a period, and a newline character (,\n
). The results are then output to the terminal.
Your mortgage is renewing soon, and the new rate is 7.876%.
Your mortgage is renewing soon, and the new rate is 7.876%.
Your mortgage is renewing soon, and the new rate is 7.876%.
Your mortgage is renewing soon, and the new rate is 7.876%.
Bonus: Putting it Together
This code loops through the list
of clients to create a customized body for each email.
clients = ['Bill', 'Micah', 'Philip', 'Josiah'] m_rates = [3.725, 4.122, 5.398, 2.409] new_rate = 7.876 i = 0 for c in clients: body_text = '' str1 = f'Dear {c},\n\n' str2 = f'{c}, I just wanted to touch base with you.\n' str3 = f'Your current mortgage rate is {m_rates[i]}%.\n' str4 = f'Your mortgage is up for renewal and new rate is {new_rate}%.\n' str5 = 'Contact me for alternate rates.\n\n' body_text += str1 + str2 + str3 + str4 + str5 print(body_text) i += 1
The result is output to the terminal.
Dear Bill,
Bill, I just wanted to touch base with you.
Your current mortgage rate is 3.725%.
Your mortgage is up for renewal and new rate is 7.876%.
Contact me for alternate rates.
Dear Micah,
Micah, I just wanted to touch base with you.
Your current mortgage rate is 4.122%.
Your mortgage is up for renewal and new rate is 7.876%.
Contact me for alternate rates.
Dear Philip,
Philip, I just wanted to touch base with you.
Your current mortgage rate is 5.398%.
Your mortgage is up for renewal and new rate is 7.876%.
Contact me for alternate rates.
Dear Josiah,
Josiah, I just wanted to touch base with you.
Your current mortgage rate is 2.409%.
Your mortgage is up for renewal and new rate is 7.876%.
Contact me for alternate rates.
Summary
This article has provided four (4) ways to interpolate strings to select the best fit for your coding requirements.
Good Luck & Happy Coding!
Programming Humor – Python
