Question Detail

COS 102 Advanced Text Processing Objective Question

Question

Given `filename = "report.txt"`, what will `filename[-4:]` produce as output?
Options
A report
B .txt
Correct Answer
C txt
D t
Correct Answer

Option B is the correct answer.

Detailed Explanation

Negative indexing starts from -1 for the last character. filename[-4:] means start 4 characters from the end and go all the way to the end. The characters are 'r'(-6), 't'(-5), '.'(-4), 't'(-3), 'x'(-2), 't'(-1). So, starting from '.' (index -4) to the end results in '.txt'.

Hint

Count 4 characters backwards from the very end of the string 'report.txt'.

Question Info