The last exercise in particular would have been much cleaner if we had a way of referring to that particular string instead of having to write it all out several times!
This is one of the basic use-cases of variables!
A variable is a way of keeping a handle on data. Variables can hold numerical or string data that we’ve encountered so far, as well any other type of data, as we’ll see later.
In order to create a variable in python, we use the assignment operator,
=
i.e. the equals sign.
For example
a_number_variable = 10
text1 = "aaaaa"
You are free to choose any name for a variable that you wish.
The only exceptions are that the variable name
cannot contain spaces or other special characters, and cannot correspond
to a special python keyword like
if
, else
, or for
, as these are reserved for special operations.
While not being illegal (illegal in programming means that it will give an error), you are also strongly advised to not over-write built-in function names.
For example it is technically legal to name a variable print
!
However, you would then overwrite the print function
and no longer be able to print things to the terminal!
Python variables are case-sensitive, so a variable
called a
cannot be referred to as A
,
and a variable called MyNumber
is not the same as mynumber
!
New programmers are sometimes confused by variables vs the data they contain, especially when it comes to string variables.
For example, the following are all valid variable assignments
one = "1"
- a variable called one
that holds the single-character string “1”one = 1
- a variable called one
that holds the number 1OnE = "one"
- a variable called OnE
that holds the string “one”Once a variable has been assigned, we can manipulate its data in exactly the same way as if we were dealing with the data (number, string, etc) directly.
For example
print("Joe Bloggs"[-6:])
and
somename = "Joe Bloggs"
print(somename[-6:])
would both output Bloggs
.
What happened here?
"Joe Bloggs"
to the variable somename
.Write a script (name the file exercise_variables.py
)
and create a variable (give it any name you like!) that contains the string
"The quick brown fox jumps over the lazy dog"
Then create a second variable that contains the text
"lazy cat"
Now use the replace
member-function to replace “lazy dog”
with the contents of the second variable and assign
the result into a third variable.
Remember that a member-function is called using the
<OBJECT>.<FUNCTION NAME>(ARGUMENTS...)
syntax.
Lastly print out all three variables.
The new part of this exercise is using the assignment operator; e.g.
AVariable = "some text"
Most of the other functionality has been covered before!
The following example achieves each of the steps:
# Testing variable assignment and string manipulation
a = "The quick brown fox jumps over the lazy dog"
b = "lazy cat"
c = a.replace("lazy dog", b)
print(a)
print(b)
print(c)
Note
Here I’ve used very short and simple variable names, a
, b
, c
- but usually
a balance between simplicity and readabilty is best!
If this were part of a big script and saw variable a
, b
, and c
, we
wouldn’t have a clue what they meant.
Instead, we could use, for example input_text
, replacement
, and result_text
.
Then if we read the script again in a year’s time, (or if our collaborator reads it)
there’s a much better chance that we (/ he/she) will understand it.
The output of the script should be
The quick brown fox jumps over the lazy dog
lazy cat
The quick brown fox jumps over the lazy cat
Along with the standard assignment operator, =
, Python has additional extensions
that provide shorthand ways to assign values into a variable.
For example (rhs = right hand side)
+=
: add the rhs to the variable; a += 10
is the same as a = a + 10
*=
: multiply rhs by the variable; a *= 2
is the same as a = a * 2
/=
: divide variable by rhs; a /= 4
is the same as a = a/4
Where appropriate, this also applies to string data, e.g.
a = "Some "
a += "text"
print(a)
would output Some text
.