What is a string?
A sequence of characters that represents text data like a person’s name, location or a user message.
Strings:
Sequence Type
Objects in a string are ordered from first to last and the position of each character is indexed starting at 0 NOT 1.
len()
An operation that can be used to find the length of a string.
String Indexing
How can I access characters within the string? [ ]
If you need to reference a character at a specific index use [ ] with the index you need. Positive numbers take from the right, Negative numbers take from the left.
e.g. print(alphabet[19]) or alphabet[-26])
updating a string
String Concatenation
You can add new characters to the end of a string using addition.
e.g. city = New + York prints NewYork
Formatted strings
What are f-strings?
These are formatted strings that can be used in print statements to pull in multiple strings or add strings together.
number=6
amount = 32
print(f”{number} coffees cost ${amount}”)
List
This is a container that is created by putting a squence of variables together with [ ].
e.g. prices = [20, 14.99, 5]
List:
Element
An item in a lists. Lists are mutable so can be replaced, reordered or removed.
Adding or removing list elements:
Append()
This adds new elements to a list.
Adding or removing list elements:
pop(Index) OR remove(“element”)
Pop() removes the element at the desired index.
Remove() removes the first element that matches(“ “)
function & methods for lists:
Picture with common list functions
Tuple (toople)
Stores a collection of data like a list but is immutable, cannot be changed.
Set()
A set is an unordered collection of elements but is UNORDERED and UNIQUE.
Sets are mutable and elements can be removed or added.
Common set() operations - picture
Dictionaries
A container that shows associatve relationships using {key: value} pairs. You access them by calling the key like {players[‘Lionel Messi’]}
e.g. {“Lionel Messi”: 10}
Data Types Summary Picture