A data type that is a sequence of characters, which can include letters, numbers, punctuation, symbols, whitespace and special characters
Strings must be enclosed in single or double quotes, e.g. 'this' or "that"
Concatenation
Joining strings together using the "+" operator
Conversion to strings
Other data types can be converted to strings, e.g. str(3.14) gives '3.14'
String repetition
Multiplying a string by an integer repeats the string, e.g. 'z' * 5 gives 'zzzzz'
Strings as sequences
Strings can be treated like sequences, allowing you to refer to characters by index, slice them, iterate through them, and check if a substring is present
String immutability
Strings are immutable - you cannot change individual characters, you can only create new strings
String methods for testing
isupper()
islower()
isspace()
isalpha()
isdigit()
isalnum()
String methods for searching
startswith()
endswith()
count()
find()
index()
replace()
String methods for manipulation
lower()
upper()
lstrip()
rstrip()
strip()
ljust()
rjust()
center()
splitlines()
split()
join()
"Chaining" methods
Joining multiple string methods together in one statement to perform a sequence of operations
Some programming languages have fewer built-in string manipulation functions compared to Python, requiring more manual approaches
Dictionaries
Another data structure in Python, like associative arrays or maps, where items are identified by unique keys rather than indexes
Dictionaries
Mutable
Keys are usually strings that act as names or labels for the values
Items have no inherent order
Creating dictionaries
1. Use curly braces {} to enclose key-value pairs
2. Separate keys and values with colons :
3. Separate multiple pairs with commas ,
Referencing dictionary items
Use the key in square brackets [] to access the corresponding value
ECU unit code
2 digits, dash or slash, 1-2 digits, dash or slash, 2-4 digits
Date
17
Dictionary
A data structure available in Python
Dictionaries
Like lists, they are mutable (you can change the content)
Often known as "associative arrays" or "maps"
Items in a dictionary are identified by a "key" that you specify
Keys are usually strings
Items in a dictionary have no order
Key-value pairs
Items in a dictionary are often referred to as "key-value pairs"
Dictionaries
A collection of variables, each with a name (key) and a value
Creating Dictionaries
1. Dictionaries are created using curly braces
2. Use the key to refer to an item in a dictionary
Adding, Changing and Removing Dictionary Items
1. Adding and changing items involves assigning a value to a key
2. Use "del" to delete an item from a dictionary, using its key
Dictionaries
The "in" comparison operator looks for a key, not a value
len() tells you how many items (key-value pairs) are in a dictionary
Dictionaries can have items and keys of different data types
Looping through Dictionaries
1. Looping through a dictionary using "for" works its way through the keys
2. You can use the "items()" method to get both the key and the value
Dictionary Methods
keys()
values()
items()
get(key[, default])
pop(key[, default])
popitem()
clear()
Dictionary Method Examples
phones.keys()
phones.values()
phones.items()
phones.get('Leisa', 'No such key.')
phones.pop('Greg', 'No such key.')
phones.popitem()
phones.clear()
Common Dictionary Usage
Dictionaries are often used to store multiple different details about a thing, rather than storing multiple named instances of the same thing
Dictionaries implement a data structure known as an "associative array" or "hashes" in many languages
Dictionaries
Dictionaries are mutable (they can be changed) and they are unordered (the items have no "position" in the dictionary)
Keys are usually strings that serve to name or label the item
The keys in a dictionary are unique – trying to add another item with the same key will just overwrite that item
KeyError is raised if you refer to a key that does not exist
Set
A data structure available in Python
Sets
The values in a set must all be unique (different)
Sets are unordered and the items have no keys or indexes
Sets are mutable – you can add and remove items
The values in a set can be of different data types
Sets are a fundamental concept of mathematics
Creating and Manipulating Sets
1. Sets can be created in two ways – curly braces or the "set()" function
2. "add()", "remove()" and "update()" manipulate the set
Working with Sets & Set Methods
len(), "in" and "for" all work as you would expect
Set methods exist for common mathematical comparisons
You can use special operators instead of the methods
There are also methods/operators to find out if a set is a subset or superset of another set
Sets are unordered collections of unique values
Sets are a data structure that many languages do not offer
When you need to keep track of a unique collection of values, sets are a very convenient way of doing so
String testing, manipulating and searching is important, and Python gives you quite a few convenient ways to do it