CBSE Class 12 Computer Science Question 10 of 145

File Handling — Question 3

Back to all questions
3
Question

Question 3

How are following statements different ?

  1. f.readline()
  2. f.readline().rstrip()
  3. f.readline().strip()
  4. f.readline.rstrip('\n')
Answer
  1. f.readline() — This statement reads a single line from the file f and returns it as a string, including any whitespace characters at the end of the line.
  2. f.readline().rstrip() — This statement first reads a single line from the file f, then applies the rstrip() method to remove any trailing whitespace from the end of the line, and returns the modified line as a string.
  3. f.readline().strip() — This statement first reads a single line from the file f, then applies the strip() method to remove any leading and trailing whitespace from the start and end of the line, and returns the modified line as a string.
  4. f.readline.rstrip('\n') — This is not a valid syntax. The correct syntax is f.readline().rstrip('\n'). This statement first reads a single line from the file f, removes any trailing newline characters from the end of the line, and returns the modified line as a string.