Computer Science
How are following statements different ?
- f.readline()
- f.readline().rstrip()
- f.readline().strip()
- f.readline.rstrip('\n')
Python File Handling
5 Likes
Answer
- 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.
- 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.
- 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.
- 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.
Answered By
2 Likes