Part 2 of Python for security, a series for security and cloud people who are new to code. Over nine short lessons we build a script that warns you before a website's TLS certificate expires. This part gives the script a variable for the site it checks.
- What it is
A variable is a name for a value
The equal sign assigns a value to a variable. In our script, the name host now refers to some text, the address of the site we want to check. A name must be assigned before it is used, or Python reports an error.
Source: Python Tutorial, section 3.1
- How it works
An f-string fills in the gap
Text in Python is a string, written inside quotes. An f-string begins with the letter f, and each name in curly brackets is replaced by that variable's value, so the script prints Checking, then the address. A capital F works too, and the braces can hold any expression, not just a name.
- Why it matters
Change it in one place
Because the f-string reads the variable, you change the site on one line and every line that uses host follows. Without a variable you would edit the address wherever it appears and could miss one. Later parts give this name a whole list of sites.
Source: Python Tutorial, section 7.1.1
The takeaway: a variable is a name for a value, an f-string drops that value into text, and changing it once changes every use.
More short lessonsADIR also offers a free external pen test for cloud teams: see the offer.
Transcript
Last time, our script printed one fixed line. Now let's teach it which site to check. This is part two. A variable is a name that refers to a value. The equal sign assigns one. Here, the name host refers to some text: the address of the site we want to check. Set it before you use it, because an undefined name gives an error. Text in Python is called a string, and it goes inside quotes. An f-string starts with the letter f. Anything inside curly brackets is swapped for that variable's value, so the output reads Checking, then the address. Why bother? Change the site in one place, and every line that uses host follows. Without a variable, you'd edit the address everywhere it appears, and could miss one. Later, we'll give this name a whole list of sites. So, a variable is a name for a value. An f-string drops that value into text. And change it once, and the whole script follows.
Narrated with an AI voice.

