Question 1: Which of the following is NOT one of the four modes for opening a file in Python, using the open() function?
The correct answer is:
- "d" (delete)
The standard modes for opening a file in Python with the open()
function are:
- "r" (read)
- "w" (write)
- "a" (append)
Question 2: The following lines of code will create a new .txt file. What will the file contain?
If you have the following lines of code:
with open('file.txt', 'w') as f:
f.write("Hello World!")
f.write("5")
The correct answer is:
- Hello World!5
The write()
method does not automatically add a newline character, so both strings will be written consecutively without any space or new line.
Question 3: If we wanted to separate parts of a text file using a specified parameter and put them in a list, we might use which Python function?
The correct answer is:
- split()
The split()
method is often used to divide a string into a list based on a specified delimiter. For example, if you have a string of text, you can split it into parts using a character like a comma or space.