Home  >  Article  >  Backend Development  >  How to remove the newline character ‘\n’ at the end of each line in python

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

尚
Original
2019-06-26 13:32:5313149browse

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')

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!

Statement:
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