COMP7004 - Systems Scripting
Lecture 16: Dictionary and String Manipulations
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
The Dictionary Data Type
Like a list, a dictionary is a collection of many values. But
unlike indexes for lists, indexes for dictionaries can use
many different data types, not just integers.
Indexes for dictionaries are called keys. A key with its
associated value is called a key-value pair.
A dictionary starts with a curly brace “{“ and ends with a
curly brace “}”.
Example:
The dictionary keys are ‘size, color, and ‘disposition. The
values for these keys are fat, gray’, and ‘loud’,
respectively.
2
Accessing Dictionary
The values in a dictionary can be accessed through their keys.
For example:
Dictionaries can still use integer values as keys, just like lists
use integer for indexes, but they do not have to start at 0 and
can be any number. For example:
3
Dictionaries vs Lists
Unlike lists, items in dictionaries are unordered. The first item
in a list will be at index 0. But there is no first item in a
dictionary.
While the order of items matters for determining whether two
lists are the same, it does not matter in which order the key-
value pairs are typed in a dictionary.
For example:
4
Dictionaries vs Lists
Because dictionaries are not ordered, they cannot be sliced like lists.
Trying to access a key that does not exist in a dictionary will result in a
KeyError error message, just like accessing none existent index in a
list.
Though dictionaries are not ordered, the fact that you can have
arbitrary data for keys allow you to organize your data in powerful
ways.
For example:
Write a program to store friends birthday
We can use the names of our friends as key and their birthday as values.
Solution: birthdays.py
5
Dictionary Example: birthday.py
6
birthday.py: Output
Once the program terminates, all the data inputted are forgotten. We have
learnt about the shelve module that can be used to persist the information by
storing the data to a file.
7
The keys(), values() and items()
Methods
These are three dictionary methods that will return
list-like values of dictionarys keys, values or both
keys and values.
The values returned by items() method are not true
list:
They cannot be modified,
They do not have an append() method.
The data structures of these methods can be nicely
used in a for loop. See following examples:
8
keys(), values() and items(): Loop
Examples
Note that the output of the
items() method is a tuple. To
get a true list equivalent, wrap
it around list() function.
9
Checking Key or Value Existence
Recall from previous lecture that the in and not in
operators can check whether a value exist in a list.
You can use also these operators to see whether a key
or value exists in a dictionary.
10
The get() Method
It can be tedious to check whether a key exist in a
dictionary before accessing the key value. Fortunately,
dictionaries have a get() method that takes two arguments:
the key of the value to retrieve,
a fall back value to return if the key does not exist.
Example:
Without the get() method, using the key ‘eggs’ could have
caused a KeyError error message.
11
The setdefault() Method
If we want to set a value for a certain key in the
dictionary that does not have a value, we usually do:
The setdefault() method offers a better way of doing
this in one line of code.
12
The setdefault() Method
The setdefault() method is a nice shortcut to ensure that a
key exist in a dictionary in order to avoid error while
manipulating dynamically.
For example:
Output:
13
Nested Dictionaries
For modelling real world data structures, dictionaries
can be nested to associate keys with a series of
values.
For example: - a nested dictionary to describe what
people brings to a picnic
14
Accessing Nested Dictionary
To access elements of a nested dictionary, we use
indexing [] strategy.
For example:
15
Manipulating Strings
16
Multiline Strings
While you can use \n escape character to put a new
line into a string, it is often easier to use multiline
strings. A multiline string in Python begins and ends
with either:
three single quotes or
three double quotes.
Any tabs, quotes or newline in between the triple
quotes” are considered part of the string.
17
Multiline String Example: catnapping.py
Notice that the single quote character in Eves does not need to
be escaped. Escaping single and double quotes in multiline
string is optional.
Output
18
Multiline Comments
While the hash character (#) marks the beginning of a
comment for the rest of the line, a multiline string is
often used for comments that span multiple lines.
For example:
19
Useful String Methods
Several string methods analyse strings or create
transformed string values. We will look at some of the
methods you might be using often.
The upper() and lower() string methods return a new string
where all the letters in the original string have been
converted to uppercase or lowercase, respectively. Non-
letter characters in the string remains unchanged.
Example:
20
Useful String Methods
The upper() and lower() are helpful if you need to make a
case-insensitive comparison.
The stringgreat and ‘GREat’ are not equal to each other.
But in the following program, it does not matter whether the
user type Great, GREAT, or greAT, because it is first converted
to lowercase.
21
Useful String Methods
The title() and capitalize() methods convert the first
character of a string word to upper case and the rest of the
characters to lower case. However, they have subtle
differences.
Example:
Note that if a word contains an apostrophe or the separator
of the words is not white space, the behaviour of these
method varies.
Example
22
Useful String Methods
The isupper() and islower() methods will return a Boolean
True value if the string contain at least one letter and all the
letters are uppercase or lowercase respectively. Otherwise,
the method returns False.
Example:
23
The isX Methods
Along with islower() and isupper(), there are several string
methods that have names beginning with the word is.
These methods return a Boolean value that describes the
nature of the string. Here are some common ones:
isalpha() returns True if the string consist of only letters
and is not blank.
isalnum() returns True if the string consist of only letters
and numbers and is not blank.
isdecimal() returns True if the string consist of only numeric
characters and is not blank.
24
The isX Methods
isspace() returns True if string consists of only spaces,
tabs, or newlines and is not blank.
istitle() returns True if the string consists of only words that
begin with an uppercase letter followed by only lowercase
letters.
The isX methods are helpful when you want to validate
user input.
This program will repeatedly
ask users for their age until
they provide valid input
25
Removing Whitespace Around
Strings
Sometimes you may want to strip off whitespace
characters (space, tabs and newline) from the left
side, right side or both sides of a string.
The strip() string method will return a new string
without any whitespace character at the beginning or
end.
The lstrip() and rstrip() methods will remove
whitespace characters from the left and right ends,
respectively.
26
Removing Whitespace Around
Strings
The following code demonstrates the methods.
27
Removing Whitespace Around
Strings
Optionally, a string argument will specify which characters
on the ends should be stripped. For example:
Passing strip() the argument ‘ampS will tell it to strip
occurrences of a, m, p, and capital S from both ends of the
string stored in cat. The order of the characters in the string
passed to strip() does not matter: strip(‘ampS’) will do the
same thing as strip(‘mapS’) or strip(‘Spam’).
28
Thank You!
29