Unit 6 - File I/O And Strings

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/52

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

53 Terms

1
New cards

String Definition

Strings are ordered sequences of characters

2
New cards

String Indexing

Accessing a single character at a specific position

3
New cards

String Slicing

Extracting a subsequence of characters

4
New cards

String Concatenation

Joining strings together using the + operator

5
New cards

String Repetition

Repeating a string multiple times using the * operator

6
New cards

String Length

Getting the number of characters using len()

7
New cards

String Immutability

Strings are immutable; operations create new strings rather than altering existing ones

8
New cards

ASCII

The original standard, using 7 bits to represent 128 characters - this includes English letters, numbers, punctuation, and control characters

9
New cards

Unicode

A much larger standard that encompasses characters from virtually all writing systems, as well as symbols and emojis

10
New cards

ord(character)

Returns the integer Unicode code of the character

11
New cards

chr(integer)

Returns the character corresponding to an integer Unicode code

12
New cards

Substring Membership (in)

Check if a substring exists within a larger string

13
New cards

Finding Substrings (find())

Returns the starting index of the first occurrence of a substring - if the substring is not found, it returns -1

14
New cards

Replacing Substrings (replace())

Returns a new string where all occurrences of a specified substring are replaced with another substring

15
New cards

.lower()

Converts all characters to lowercase

16
New cards

.upper()

Converts all characters to uppercase

17
New cards

.capitalise()

Capitalises the first character of the string.

18
New cards

.title()

Capitalises the first character of each word

19
New cards

.swapcase()

Swaps the case of all characters

20
New cards

.ljust(n)

Left-justifies the string in a field of width n, padding with spaces on the right

21
New cards

.rjust(n)

Right-justifies the string in a field of width n, padding with spaces on the left

22
New cards

.center(n)

Centres the string in a field of width n, padding with spaces on both sides

23
New cards

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

24
New cards

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

25
New cards

Joining Syntax

separator.join(iterable)

26
New cards

Splitting Lines (splitlines())

A convenient way to split a string into a list of lines, respecting different newline characters

27
New cards

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

28
New cards

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

29
New cards

Interchange In Files

Files allow different programs to communicate and share data

30
New cards

Organisation In Files

File systems (directories and folders) provide a hierarchical structure for organising information

31
New cards

Large Data In Files

Files enable working with datasets that are too large to fit into the computer’s main memory

32
New cards

Files

Files provide a mechanism for long-term data storage, inter-program communication, and structured organisation

33
New cards

Reading Files

Fetching data sequentially from the file

34
New cards

Writing To Files

Appending data sequentially to the file

35
New cards

Seek (In Files)

Moving the current position within the file

36
New cards

State (In Files)

Streams have a “position” that tracks how far you are into the file - reading or writing updates this position

37
New cards

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

38
New cards

“r”

Read Mode (default) - opens a file for reading - if the file doesn’t exist, it raises a FileNotFoundError

39
New cards

“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

40
New cards

“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

41
New cards

“r+”

Read/Write Mode - opens a file for both reading and writing

42
New cards

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

43
New cards

file.read()

Reads from the current position to the end of the file

44
New cards

file.read(n)

Reads at most n characters from the file

45
New cards

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

46
New cards

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

47
New cards

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

48
New cards

write(s)

Writes a string s to the file at the current position

49
New cards

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

50
New cards

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

51
New cards

strip()

This string method removes all leading and trailing whitespace characters from a string

52
New cards

lstrip()

Removes only leading (left-side) whitespace

53
New cards

rstrip()

Removes only trailing (right-side) whitespace