How to remove the newline character '\n' at the end of each line in python

Release: 2019-07-08 09:01:04
Original
13321 people have browsed it

How to remove the newline character '\n' at the end of each line in python

The program is as follows:

for line in file.readlines():
 line=line.strip('\n')
Copy after login

Use the strip() function to remove the \n at the end of each line

strip() function prototype

Statement: str is a string, chars is the character sequence to be deleted

str.strip(chars): delete the characters at the beginning and end of the s string, located in the chars deletion sequence

str.lstrip(chars): Delete the characters at the beginning of the s string, located in the chars deletion sequence

str.rstrip(chars): Delete the characters at the end of the s string, located in the chars deletion sequence

Note:

When chars is empty, whitespace characters (including '\n', '\r', '\t', ' ') are deleted by default.

More For Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to remove the newline character '\n' at the end of each line in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template