1/52
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
String Definition
Strings are ordered sequences of characters
String Indexing
Accessing a single character at a specific position
String Slicing
Extracting a subsequence of characters
String Concatenation
Joining strings together using the + operator
String Repetition
Repeating a string multiple times using the * operator
String Length
Getting the number of characters using len()
String Immutability
Strings are immutable; operations create new strings rather than altering existing ones
ASCII
The original standard, using 7 bits to represent 128 characters - this includes English letters, numbers, punctuation, and control characters
Unicode
A much larger standard that encompasses characters from virtually all writing systems, as well as symbols and emojis
ord(character)
Returns the integer Unicode code of the character
chr(integer)
Returns the character corresponding to an integer Unicode code
Substring Membership (in)
Check if a substring exists within a larger string
Finding Substrings (find())
Returns the starting index of the first occurrence of a substring - if the substring is not found, it returns -1
Replacing Substrings (replace())
Returns a new string where all occurrences of a specified substring are replaced with another substring
.lower()
Converts all characters to lowercase
.upper()
Converts all characters to uppercase
.capitalise()
Capitalises the first character of the string.
.title()
Capitalises the first character of each word
.swapcase()
Swaps the case of all characters
.ljust(n)
Left-justifies the string in a field of width n, padding with spaces on the right
.rjust(n)
Right-justifies the string in a field of width n, padding with spaces on the left
.center(n)
Centres the string in a field of width n, padding with spaces on both sides
Splitting (split())
Breaks a string into a list of substrings based on a specified delimiter - if no delimiter is provided, it splits on any whitespace character (space, tab, newline) and discards empty strings
Joining (join())
Takes an iterable (like a list) of pixels and concatenates them into a single string, using the string it’s called on as a separator
Joining Syntax
separator.join(iterable)
Splitting Lines (splitlines())
A convenient way to split a string into a list of lines, respecting different newline characters
Efficiency Of Combining Strings
For joining a large number of strings, join() is significantly more performant than using the + operator repeatedly, as + creates many intermediate string objects
Persistence In Files
Data in memory is lost when a program ends or the computer turns off - files save data so it can be used later
Interchange In Files
Files allow different programs to communicate and share data
Organisation In Files
File systems (directories and folders) provide a hierarchical structure for organising information
Large Data In Files
Files enable working with datasets that are too large to fit into the computer’s main memory
Files
Files provide a mechanism for long-term data storage, inter-program communication, and structured organisation
Reading Files
Fetching data sequentially from the file
Writing To Files
Appending data sequentially to the file
Seek (In Files)
Moving the current position within the file
State (In Files)
Streams have a “position” that tracks how far you are into the file - reading or writing updates this position
Context Managers (with open(…) as f:)
It automatically ensures that the file is properly closed after you’re done with it, even if errors occur - this is crucial for releasing system resources
“r”
Read Mode (default) - opens a file for reading - if the file doesn’t exist, it raises a FileNotFoundError
“w”
Write Mode - opens a file for writing - crucially, this will erase the contents of the file if it already exists - if the file doesn’t exist, it creates a new one
“a”
Append Mode - opens a file for writing, but adds new data to the end of the file - if the file doesn’t exist, it creates a new one
“r+”
Read/Write Mode - opens a file for both reading and writing
read()
Reads the entire content of the file into a single string - be cautious with very large files, as this can consume a lot of memory
file.read()
Reads from the current position to the end of the file
file.read(n)
Reads at most n characters from the file
readLine()
Reads a single line from the file, including the newline character (\n) at the end (if present) - it returns an empty string when the end of the file is reached
readlines()
Reads all lines from the file and returns them as a list of strings - each string in the list includes the trailing newline character
Natural Iteration (using a for loop)
This is often the most Pythonic and memory-efficient way to process as file line by line, especially for large files - the file object itself acts as an iterator
write(s)
Writes a string s to the file at the current position
writelines(list_of_strings)
Writes each string from the provided list (or any iterable) to the file - similar to write(), it does not add newlines automatically
print(…, file=f)
Write formatted output to a file, mimicking the behaviour of the standard print() function - it automatically adds a newline character at the end unless specified otherwise
strip()
This string method removes all leading and trailing whitespace characters from a string
lstrip()
Removes only leading (left-side) whitespace
rstrip()
Removes only trailing (right-side) whitespace