Using Variables

Updated on 28 Dec 2022

Basic example

Variables are very useful for holding pieces of information that are variable. I.e. the value might change. Simple examples include mathematical expressions, counters and even reading the contents of a file into a variable that you can then use in your script.

Let’s look at a simple example.

petName = 'sooty'
petAge  = 14    # this is an integer
oldPet  = '24'  # this is a string with value 24

#technique 1
print('The pet name is {0} and the age of the pet is {1}'.format(petName, petAge))

#technique 2
print('The pet name is ' + petName + ' and the age of the pet is ' + str(petAge))

#example
print('The pet name is ' + petName + ' and it is an old age at ' + oldPet)

Note: you can only use + to concatenate strings. Number variables will need to be converted to strings with str

Numbers

When we have petAge = 14, this is a number variable. When the number is quoted (like we did with the example above with 24), then it is a string. Number variables are important because we can do maths stuff with them.

petAge = 14
petAge = petAge + 1

print('age of pet: {age}'.format(age=petAge))

In this example I have used a named parameter. Earlier I use {0} & {1}; which means that I need to put my variables in the correct order that I want them used in the string. Using a named parameter I can put them in any order and reference them by name.

Tip: a variable should be a number if you are going to do maths on it. A phone number and postcodes are numbers, but we don’t do maths with them. Therefore they should be declared as strings (i.e. quoted).

Strings

Strings contain numbers and letters plus other characters that appear on your keyboard like dashes, underscores and even spaces. When we have a variable that contains a string, we can do interesting stuff with it.

myName = 'brent'
myName = myName.upper()

print(myName)

You might be wondering how I knew about the function str.upper(). Well, I simply followed this path

Then it is a matter of scrolling down the list of functions until I found one that matches my needs.

Strings - guided exercise

Imagine that we have a string that contains a file name, and we want to know what the file extension is.

filename = 'myFile.pdf'

Looking at this piece of code I would think that I am looking for a solution where I can split the string on the dot, and the stuff to the right is the extension. I.e. pdf. It just so happens that when I search the Text Sequence Type documentation there is infact a split method.

filename = 'myFile.pdf'

parts = filename.split('.')
print(parts)  #I'll just print it out and see what it looks like

That is interesting, it has split, but how do I access the bit that says pdf? We cover that bit in later sections called lists, but for now we can just use parts[1]

filename = 'myFile.pdf'

parts = filename.split('.')
print(parts[1])  #I added the [1] to parts.

Info: Sequences start at 0.

In our example we could use parts[0] & parts[1], but if we tried to use 2 or higher we will get a ‘list index out of range’ error. The top range that we can use is parts['number_of_parts - 1']. If you have a quick look at the documentation for lists we will find a function call len() that we can use to tell us how many elements there are inside a list.

Strings - extended exercise

In the example code above, what would happen if the filename had been declared like this:

filename = 'my.File.pdf'

Can we use the information we’ve learnt so far to come up with a better solution? One that will work for these types of scenario’s

  • myFile.pdf
  • my.File.pdf

Hint We will need to use list operations with the solution.