line.strip()
时间: 2024-05-08 15:16:27 浏览: 77
The `strip()` method in Python removes any leading and trailing whitespace characters (such as spaces, tabs, and newlines) from a string.
`line.strip()` is a common use case in Python when processing text files. It is used to remove any unwanted whitespace characters from the beginning and end of each line read from a file.
For example, consider the following code snippet:
```
with open('example.txt', 'r') as f:
for line in f:
line = line.strip()
# do something with the cleaned up line
```
In this code, we are opening a file named `example.txt` and reading its contents line by line with a loop. The `strip()` method is called on each line to remove any leading or trailing whitespace characters, and the cleaned up line is then processed in some way.
阅读全文